2016-12-25 6 views
0

私はphpとAJAXを使って簡単なフォームを作成しました。データが送信されると、行はデータベースに追加されますが、リフレッシュされない限りページには表示されません。PHPとAJAXが動作しないMYSQLに挿入する

エラーは表示されません。

更新:私が成功に追加した警告が表示されています。しかし、データはまだページリロードなしでは表示されません。

何か助けがありがとうございます。

AJAX:

$("form#message_form").submit(function() { 

    var form = $(this); 
    var url = "message.php"; // the script where you handle the form input. 

    $.ajax({ 
      type: "POST", 
      url: url, 
      data: $("input.input_styling").serialize(), // serializes the form's elements. 
      dataType: 'html', 
      success: function(data) 
      { 

       alert('Checking if working...'); 

      } 
     }); 

    return false; // avoid to execute the actual submit of the form. 

}); 

HTML:

<form action="" id="message_form" style="text-align:right;margin:0;"> 
    <input class="input_styling" type="text" name="infomation" /> 
    <button name="submit" class="submit_icon" type="submit"> > </button>  
</form> 

message.php(本当に必要はないが、念のため)

$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line 

$sql = "INSERT INTO test (id, infomation) 
VALUES ('".$_POST["id"]."','".$_POST["infomation"]."')"; 


if ($dbh->query($sql)) { 

} 
else{ 
echo "Ops! something went wrong..."; 
} 

$dbh = null; 


?> 

答えて

0

さて私はちょうどこれを実行し、それが働きました...私が変更したのはURLだけなので、ファイルを正しくリンクしているかどうかを確認してください。 退屈な質問--JQueryを含めましたか?

$("form#message_form").submit(function() { 

    var form = $(this); 
    var url = "http://httpbin.org/post"; // the script where you handle the form input. 

    $.ajax({ 
     type: "POST", 
     url: url, 
     data: $("input.input_styling").serialize(), // serializes the form's elements. 
     dataType: 'html', 
     success: function(data) 
     { 
      alert('Checking if working...'); 
     } 
    }); 

    return false; // avoid to execute the actual submit of the form. 

}); 
0

JSを次のように変更します。

$("form#message_form").submit(function() { 
 

 
    var form = $(this); 
 
    var url = "message.php"; // the script where you handle the form input. 
 

 
    $.ajax({ 
 
      type: "POST", 
 
      url: url, 
 
      data: $("form#message_form").serialize(), // serializes the form's elements. 
 
      dataType: 'html', 
 
      success: function(data) 
 
      { 
 

 
       alert('Checking if working...'); 
 

 
      } 
 
     }); 
 

 
    return false; // avoid to execute the actual submit of the form. 
 

 
});

あなただけの "INFOMATION" 値が必要な場合は、

$("form#message_form").submit(function() { 
 

 
    var form = $(this); 
 
    var url = "message.php"; // the script where you handle the form input. 
 

 
    $.ajax({ 
 
      type: "POST", 
 
      url: url, 
 
      data: "information=" + $("input[name=information]").val(), // Get Information Value 
 
      dataType: 'html', 
 
      success: function(data) 
 
      { 
 

 
       alert('Checking if working...'); 
 

 
      } 
 
     }); 
 

 
    return false; // avoid to execute the actual submit of the form. 
 

 
});

関連する問題