如何在 PHP 中处理从 jQuery 序列化并发送的数据?
我正在努力获取 $.ajax 调用以正确地将一些表单数据发送到 PHP(将其记录到数据库然后显示)。我很困惑,因为我之前用 $.ajax 来完成类似的任务,而且效果很好,但我一定在这里遗漏了一些关键的东西。我研究了其他答案(例如 这个),但找不到任何表明我的答案的内容当前的代码将无法工作。任何见解将不胜感激!
表单如下所示:
<div id="note_add_container">
<form id="note_add" method="POST">
<input type="text" name="title" placeholder="title" />
<input type="text" name="summary" placeholder="summary" />
<input type="text" name="details" placeholder="details" />
<button id="submit_note">Add note!</button>
</form>
</div>
<div id="entries">
<!-- AJAX call will populate entries here -->
</div>
这是 jQuery:
$('#submit_note').click(function () {
var text = $.ajax ({
type: "POST",
url: "note_process.php",
data: $('#note_add').serialize(),
dataType: "json",
async: false,
}).responseText;
$('#entries').html(text);
})
这是 PHP note_process.php:
include_once "connect.php";
session_start();
$id = $_SESSION['userid'];
$title = $_POST['title'];
$summary = $_POST['summary'];
$details = $_POST['details'];
$query = mysql_query("INSERT INTO notes (id, title, summary, details) VALUES ('$id', '$title','$summary','$details')");
echo $title . $summary . $details;
I'm struggling to get a $.ajax call to correctly send some form data to PHP (where it gets recorded to a database and then displayed). I'm stumped because I've $.ajax to do similar tasks before and it's worked great but I must be missing something critical here. I've researched other answers (such as this one) but can't find anything there that suggests my current code would not work. Any insight would be greatly appreciated!
The form looks like this:
<div id="note_add_container">
<form id="note_add" method="POST">
<input type="text" name="title" placeholder="title" />
<input type="text" name="summary" placeholder="summary" />
<input type="text" name="details" placeholder="details" />
<button id="submit_note">Add note!</button>
</form>
</div>
<div id="entries">
<!-- AJAX call will populate entries here -->
</div>
Here is the jQuery:
$('#submit_note').click(function () {
var text = $.ajax ({
type: "POST",
url: "note_process.php",
data: $('#note_add').serialize(),
dataType: "json",
async: false,
}).responseText;
$('#entries').html(text);
})
Here is the PHP note_process.php:
include_once "connect.php";
session_start();
$id = $_SESSION['userid'];
$title = $_POST['title'];
$summary = $_POST['summary'];
$details = $_POST['details'];
$query = mysql_query("INSERT INTO notes (id, title, summary, details) VALUES ('$id', '$title','$summary','$details')");
echo $title . $summary . $details;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试使用“成功”,而不是使 Ajax 请求非异步...
Try using 'success', instead of making the Ajax request not-async...
如果仍然无法正常工作,我会担心您的网址是否准确...
尝试使用 PHP 文件的绝对路径...例如“/note_process.php”(或文件相对于站点根目录的位置)
If things still aren't working, I'd be weary of whether your url is accurate...
Try using an absolute path to your PHP file instead... such as "/note_process.php" (or wherever the file is located relative to your site root)
这是一个猜测:
title
列在您的notes
表中是required
,$title = $_POST['type'];
中使用了错误的 post 参数,因此:
$title = $_POST['type'];
应该生成警告,但它没有,$title
将为空,您的查询无法插入记录,您会收到致命错误,但看不到它。This is a guess:
title
column isrequired
in yournotes
table,$title = $_POST['type'];
So:
$title = $_POST['type'];
should generate a warning, but it doesn't,$title
will be blank and your query fails to insert the record and you get a fatal error, but do not see it.我对 jsFiddle 的发现:
将 BUTTON 标记替换为 A 标记,效果很好。
Jordan Rynard 的观点很好:您确定您的 URL 有效且正确吗?
My findings on jsFiddle: the
<button>
seems to be submitting the form!? God knows why!? The solution is fairly simple =) Don't use a<button>
Replace the BUTTON tag with an A tag and it works fine.
Jordan Rynard has good point: you sure your URL is valid and correct?