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