2016-12-25 47 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. 
 

 
});

,如果你只需要 「信息來源」 值;

$("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. 
 

 
});