我用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;
?>