2012-04-16 13 views
2

私の解決策は、PHPページを呼び出してパラメータを投稿し、AJAXを使用して結果を返すonClickイベントを使用しています。指定されたURLを使用してPHPページを呼び出すときにパラメータを挿入します。ここ は私のJavascriptです:PHPページをJavascriptから呼び出すときのAJAXの返信エラーOnClick

function uploadContact() { 
     var name, email, phone, badgeid; 
     if(window.localStorage.length > 0) 
     { 
      name = window.localStorage.getItem('name'); 
      email = window.localStorage.getItem('email'); 
      phone = window.localStorage.getItem('phone'); 
     } 
     else 
     { 
      name = $('input[name=fullname]').val(); 
      email = $('input[name=email]').val(); 
      phone = $('input[name=phone]').val(); 
     } 
     $.ajax({ 
      url: 'UpdateContactList.php?name=' + name + '&email=' + email + '&phone=' + phone, 
      type: $form.attr('method'), 
      dataType: 'json', 
      success: function(responseJson) { 
      $form.before("<p>"+responseJson.message+"</p>"); 
     }, 
     error: function() { 
      $form.before("<p>There was an error processing your request.</p>"); 
     }}); 
    } 

私のPHPコードは、パラメータを取得するために:私は、閲覧アドレスからのパラメータを直接PHPページを呼び出すことができます

  1. <?php 
    
    $response = array(); 
    
    if($_SERVER['REQUEST_METHOD'] == 'POST') 
    { 
        // if form has been posted process data 
    
        // you dont need the addContact function you jsut need to put it in a new array 
        // and it doesnt make sense in this context so jsut do it here 
        // then used json_decode and json_decode to read/save your json in 
        // saveContact() 
        $data = array('fullname' => $_POST['name'], 'email' => $_POST['email'], 'phone' => $_POST['phone']); 
         //These line is for testing only, remove it when done testing 
        $test = $_POST['name'] . ' ' . $_POST['email']; 
        echo "<script>alert('$test');</script>"; 
        // always return true if you save the contact data ok or false if it fails 
        $response['status'] = updateContact($data) ? 'success' : 'error'; 
        $response['message'] = $response['status'] 
          ? 'Your submission has been saved!' 
          : 'There was a problem saving your submission.'; 
    
        header("Content-type: application/json"); 
        echo json_encode($response); 
        exit; 
    } 
    ... 
    function updateCacheFile($filename) 
    { 
        $filename = "contact"; 
        $filename = $filename . ".appcache"; 
        $cachefile = fopen ($filename, "a+"); 
        .... 
        file_put_contents($filename, $cache_content); 
    } 
    

    を2つの問題点を注意してくださいリンクすると、何も返されません。それは普通ですか?

  2. javascriptから呼び出します。テストライン(アラートメッセージボックスを呼び出す)は機能せず、メッセージは表示されません。しかし、まだ更新されたキャッシュファイルは、PHPページが呼び出され、ファイルを書き込むようなことをします。

このPHPページには2つのファイルがあります。どちらも正しく書かれていますが、パラメータは表示されず、メッセージボックスは表示されません。

ここで何が問題になったのか教えてください。

P/S:細部、スペースのあるパラメータは正しく渡すことができますか?私は、ユーザーが「Bill Jobs」などのフルネームを入力することを期待していたからです。

Thanks to Stackoverflowコミュニティ。あなたがecho INGを開始するか、そうでない場合は、出力をフラッシュする前に

答えて

2
echo "<script>alert('$test');</script>"; 
    // always return true if you save the contact data ok or false if it fails 
    $response['status'] = updateContact($data) ? 'success' : 'error'; 
    $response['message'] = $response['status'] 
      ? 'Your submission has been saved!' 
      : 'There was a problem saving your submission.'; 

    header("Content-type: application/json"); 
    echo json_encode($response); 

すべてheader sが実行されなければなりません。

header("Content-type: application/json"); 
    echo "<script>alert('$test');</script>"; 
    // always return true if you save the contact data ok or false if it fails 
    $response['status'] = updateContact($data) ? 'success' : 'error'; 
    $response['message'] = $response['status'] 
      ? 'Your submission has been saved!' 
      : 'There was a problem saving your submission.'; 

    echo json_encode($response); 
+0

私は今それを得ました。このケースでは、すべてのことが正しく行われ、ファイルが作成され、パラメータが渡されました。テストラインが削除され、エコーが不要になりました。しかし、1つのこと:私はいつも「リクエストを処理する際にエラーが発生しました。 AJAXリターンでそれはどうしたらできますか? – Shinigamae

2
$.ajax({ 
     url: 'UpdateContactList.php', 
     data:{'name':name,'email': email,'phone':phone,'badgeid ':badgeid }, 
     type: $form.attr('method'), 
     dataType: 'json', 
     success: function(responseJson) { 
     $form.before("<p>"+responseJson.message+"</p>"); 
    }, 
    error: function() { 
     $form.before("<p>There was an error processing your request.</p>"); 
    }}); 
+0

正しく完了しました。ありがとう@MiniduM – Shinigamae

1

AJAXのデータのparamであなたの情報を送信し、投稿するタイプを設定してみてください。このように...

$.ajax({ 
     url: 'UpdateContactList.php', 
     type: 'post', 
     dataType: 'json', 
     data: {'key1': value1, 'key2':value2}, 
     success: function(responseJson) { 
     $form.before("<p>"+responseJson.message+"</p>"); 
    }, 
+0

私は今それをやっています、ありがとう@lascort – Shinigamae

関連する問題