如何在 PHP 中处理从 jQuery 序列化并发送的数据?

发布于 2024-11-07 13:04:22 字数 1625 浏览 0 评论 0原文

我正在努力获取 $.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

宫墨修音 2024-11-14 13:04:22

尝试使用“成功”,而不是使 Ajax 请求非异步...

   $('#submit_note').click(function () {
        $.ajax ({
            type: "POST",
            url: "note_process.php",
            data: $('#note_add').serialize(), 
            success: function(text){
                  $('#entries').html(text); 
            }
        });    
    });

Try using 'success', instead of making the Ajax request not-async...

   $('#submit_note').click(function () {
        $.ajax ({
            type: "POST",
            url: "note_process.php",
            data: $('#note_add').serialize(), 
            success: function(text){
                  $('#entries').html(text); 
            }
        });    
    });
浅黛梨妆こ 2024-11-14 13:04:22

如果仍然无法正常工作,我会担心您的网址是否准确...
尝试使用 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)

知足的幸福 2024-11-14 13:04:22

这是一个猜测:

  1. 您关闭了错误报告(在开发阶段时错误)
  2. title 列在您的 notes 表中是 required
  3. (此是事实)您在 $title = $_POST['type']; 中使用了错误的 post 参数

,因此:

  1. $title = $_POST['type'];应该生成警告,但它没有,
  2. $title 将为空,您的查询无法插入记录,您会收到致命错误,但看不到它。

This is a guess:

  1. You have error reporting turned off (bad while in dev phase)
  2. title column is required in your notes table,
  3. (this is a fact) you are using the wrong post parameter in $title = $_POST['type'];

So:

  1. $title = $_POST['type']; should generate a warning, but it doesn't,
  2. $title will be blank and your query fails to insert the record and you get a fatal error, but do not see it.
绅士风度i 2024-11-14 13:04:22

我对 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文