[英]jquery ajax database won't update
this is my first post on Stackoverflow! 这是我关于Stackoverflow的第一篇文章!
So I'm trying to execute an "Insert" query via Jquery's $.ajax function. 因此,我试图通过Jquery的$ .ajax函数执行“插入”查询。 Looking at the network tab on chrome dev tools, it seems to be loading my file but not updating the database.
查看chrome dev工具上的网络标签,似乎正在加载我的文件,但未更新数据库。
Without any ajax, this code works fine: 没有任何ajax,此代码可以正常工作:
<form id="alForm" name="alForm" method="POST" action="add.php">
However, this takes me to the blank php page. 但是,这将我带到空白的php页面。 I don't want that.
我不要
Let me share my code! 让我分享我的代码!
Here is the PHP: 这是PHP:
<?php
error_reporting(E_ALL);
$user = 'root';
$pass = '';
try {
$db = new PDO('mysql:host=localhost;dbname=utility_portal', $user, $pass);
if (isset($_POST['save'])) {
$t = $db->prepare("INSERT INTO `u_links` (`title`, `link`) VALUES (?, ?)");
$t->execute(array($_POST['title'], $_POST['link']));
}
} catch(PDOException $e) {
echo "Error!: " . $e->getMessage() . "<br />" ;
die();
}
?>
Here is the Jquery: 这是Jquery:
$('#alForm').on('submit', function(event) {
$.ajax({
url : "add.php",
type : "POST",
data: $('#alForm').serialize(),
success: function(data) {
alert('submitted ' + data);
}
});
event.preventDefault();
});
And the HTML: 和HTML:
<form id="alForm" name="alForm">
<label>Title</label>
<input type="text" name="title" placeholder="Title"/>
<label>Link</label>
<input type="text" name="link" placeholder="Link"/>
<input type="submit" name="save" id="save" class="btn btn-primary" value="Save" />
</form>
Everything looks fine to me. 对我来说一切都很好。 Am I missing something?
我想念什么吗? Thanks for the help.
谢谢您的帮助。
I think your problem is the "serialize" part. 我认为您的问题是“序列化”部分。 If you remove the line the code should work.
如果您删除该行,则代码应该可以正常工作。 You can also use the shortcut code:
$.post("add.php", success: function(data) { alert('submitted ' + data); }
您还可以使用快捷方式代码:
$.post("add.php", success: function(data) { alert('submitted ' + data); }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.