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 Python语言实验
  • ¥15 SAP HANA SQL 增加合计行
  • ¥20 用C#语言解决一个英文打字练习器,有偿
  • ¥15 srs-sip外部服务 webrtc支持H265格式
  • ¥15 在使用abaqus软件中,继承到assembly里的surfaces怎么使用python批量调动
  • ¥15 大一C语言期末考试,求帮助🙏🙏
  • ¥15 ch340驱动未分配COM
  • ¥15 Converting circular structure to JSON
  • ¥30 Hyper-v虚拟机相关问题,求解答。
  • ¥15 TSM320F2808PZA芯片 Bootloader