2012-01-05 13 views
0

jqueryを使用してjson postからnullを受け取っています。以下の私のコードを参照してください。

ユーザーは、このフォームに記入し、送信します

<form name="newsletterSignup" id="newsletterSignup" method="post"> 

     <input type="text" name="firstName" id="firstName" type="text" autocomplete="off" placeholder="Enter your name" /> 

     <input type="text" name="email" id="email" type="text" autocomplete="off" placeholder="Enter a valid email address"/> 

     <input type="submit" name="newsletterSubscribe" id="newsletterSubscribe" value="Sign me up now!" /> 

    </form> 

その後、これが実行されます。

var BASE_URL = 'http://www.thegurucoder.com/'; 

$('#newsletterSignup').submit(function() 
{ 

    var ajaxURL = BASE_URL + '_php/ajax/newsletterSubscribe.php'; 

    $.ajax({ 
     type: 'POST', 
     url: ajaxURL, 
     data: { firstName:$('#newsletterSignup #firstName').val(), email:$('#newsletterSignup #email').val()}, 
     dataType: 'json', 
     success: function(data){ 

      alert(data); 

      if(data.redirect == 'true') 
      { 

       window.location = data.url; 

      } 
      else 
      { 

       $(this).notify({prependTo:'#master', type:'negative', message:data.message, delay:3200, animation:'slide'}); 

      } 

     } 
    }); 

    return false; 

}); 

今度はこの実行する:

function newsletterSubscribe() 

{ 

if(isset($_POST['newsletterSubscribe'])) 
{ 

    header('Content-Type: application/json'); 

    $db = new Connection(DB_HOST, DB_USER, DB_PASS, DB_NAME); 

    $firstName = $_POST['firstName']; 
    $email = $_POST['email']; 

    if($email != '') 
    { 

     if(isDuplicateValue('subscribers', 'email', $email)) 
     { 

      echo json_encode(array(
       'message' => 'You have already subscribed to this newsletter.', 
       'redirect' => 'false' 
      )); 

     } 
     else 
     { 

      $parameters = array(
       'table' => 'subscribers', 
        'fieldsAndValues' => array(
        'firstName' => $firstName, 
        'email' => $email, 
        'addedDate' => datetime() 
        ) 
      ); 

      $db->insert($parameters); 

      echo json_encode(array(
       'redirect' => 'true', 
       'url' => BASE_URL.'subscribed' 
      )); 

     } 

    } 
    else 
    { 

     echo json_encode(array(
      'message' => 'You did not enter a valid email address.', 
      'redirect' => 'false' 
     )); 

    } 

} 

} 

を、問題は次のとおりです。

未知のタイプのエラー:nullのプロパティ 'redirect'を読み取ることができません

これは、インスペクタで取得しているヘッダです。

Request URL:http://www.thegurucoder.com/_php/ajax/newsletterSubscribe.php 
Request Method:POST 
Status Code:200 OK 
Request Headersview source 
Accept:application/json, text/javascript, */*; q=0.01 
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3 
Accept-Encoding:gzip,deflate,sdch 
Accept-Language:en-US,en;q=0.8 
Connection:keep-alive 
Content-Length:17 
Content-Type:application/x-www-form-urlencoded 
Cookie:PHPSESSID=f3015d3c6fa08e53fc562477da0f563f 
Host:www.thegurucoder.com 
Origin:http://www.thegurucoder.com 
Referer:http://www.thegurucoder.com/blog 
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.63 Safari/535.7 
X-Requested-With:XMLHttpRequest 
Form Dataview URL encoded 
firstName: 
email: 
Response Headersview source 
Cache-Control:no-store, no-cache, must-revalidate, post-check=0, pre-check=0 
Connection:close 
Content-Encoding:gzip 
Content-Language:en-UK 
Content-Length:20 
Content-Type:text/html; charset=UTF-8 
Date:Thu, 05 Jan 2012 15:21:54 GMT 
Expires:Thu, 19 Nov 1981 08:52:00 GMT 
Pragma:no-cache 
Server:Apache/2.2.16 (Unix) mod_ssl/2.2.16 OpenSSL/0.9.8e-fips-rhel5 mod_bwlimited/1.4 
Vary:Accept-Encoding 
X-Powered-By:PHP/5.2.14 

答えて

1

あなたのコードの問題は、あなたがあなたが「newsletterSubscribe」をチェックしているAJAX要求ではなく、そのPHPファイルでPHPのファイルに「newsletterSubscribe」フィールドを掲示していないということであるかの条件(ISSETかのように($ _POST ['newsletterSubscribe']))。

PHPコードではif(isset($_POST['newsletterSubscribe']))からif(isset($_POST['firstName']))に変更されており、それは確実に機能します。

+0

ロック!私はこのような問題を嫌い、カンマが足りないのが好きです。助けてくれてありがとう!!!!!実際のフォームから変換されていないajaxのでこれを完全に逃した!再度、感謝します! –

+0

LOL、仲間さん:) –

1

名前とメールフィールドが設定されていません。 ajaxリクエストの外でそれらの値を取得するためにjquery関数を削除してみてください。現在、コードをデータハッシュの一部として渡しています。

var first_name = $('#newsletterSignup > #firstName').val(); 
var email = $('#newsletterSignup > #email').val(); 
$.ajax({ 
     type: 'POST', 
     url: ajaxURL, 
     data: { firstName:first_name, email:email}, 
     dataType: 'json', 
     ... 
}); 

+0

ありがとうございましたが、とにかく解決された問題が設定されています! –

関連する問題