dongzhiju0324 2011-05-15 20:03
浏览 61
已采纳

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

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;
  • 写回答

4条回答 默认 最新

  • douxi3404 2011-05-15 20:11
    关注

    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); 
                }
            });    
        });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?