用于在数据库中插入数据的jQuery .ajax查询无效

时间:2012-12-14 15:17:25

标签: php javascript jquery ajax

我正在构建一个使用jQuery / AJAX将数据发送到php页面的网站,并从那里将其插入到数据库中。出于某种原因,代码没有插入,我根本没有回复。

我的javascript:

    function insert_data(){
    var title = debate_title.value;
    var subtitle = debate_sub.value;
    var sides = debate_sides.value;

    $(function() {
        $.ajaxSetup({
            error: function(jqXHR, exception) {
                if (jqXHR.status === 0) {
                    window.location.replace('errors/noConnection.html');
                } else if (jqXHR.status == 404) {
                    window.location.replace('errors/noConnection.html');
                } else if (jqXHR.status == 500) {
                    window.location.replace('errors/noConnection.html');
                } else if (exception === 'parsererror') {
                    window.location.replace('errors/noConnection.html');
                } else if (exception === 'timeout') {
                    window.location.replace('errors/noConnection.html');
                } else if (exception === 'abort') {
                    window.location.replace('errors/noConnection.html');
                } else {
                    window.location.replace('errors/noConnection.html');
                }
            }
        });
    });

    $.ajax({
        type: "POST",
        url: "post_debate.php",
        data: { post_title: title, post_sub: subtitle, post_sides: sidesm, ajax: 1 },
        dataType: "json",
        timeout: 5000, // in milliseconds
        success: function(data) {
            if(data!==null){
                window.location.replace('show_debate.php?id=' + data);
            }else{
                window.location.replace('errors/noConnection.html');
            }
        }
    });
}

我的PHP代码(post_debate.php):

    <?php

    require('connect.php');

    $title = $_POST['post_title'];

    $subtitle = $_POST['post_sub'];

    $sides = $_POST['post_sides'];

    $ajax = $_POST['ajax'];

    $date = new DateTime();
    $timeStamp = $date->getTimeStamp();

    if($ajax==1){
        $query = mysql_query("INSERT INTO debates VALUES('','$title','$subtitle','$sides','0','0','$timeStamp')");
        $get_data = mysql_query("SELECT id FROM debates WHERE title='$title', subtitle='$subtitle', sides='$sides', timestamp='$timeStamp'");
        while($id=mysql_fetch_array($get_data)){
            $final_id = $id['id'];
        }
        exit($final_id);
    }else{
        die("404 SERVER ERROR");
    }

?>

谢谢!


编辑 - 未解决

我的新PHP代码:

<?php
header("content-type: application/json");

require('connect.php');

$title = $_POST['post_title'];

$subtitle = $_POST['post_sub'];

$sides = $_POST['post_sides'];

$ajax = $_POST['ajax'];

$date = new DateTime();
$timeStamp = $date->getTimeStamp();

if($ajax==1){
    $query = mysql_query("INSERT INTO debates VALUES('','$title','$subtitle','$sides','0','0','$timeStamp')");
    $get_data = mysql_query("SELECT id FROM debates WHERE title='$title', subtitle='$subtitle', sides='$sides', timestamp='$timeStamp'");
    while($id=mysql_fetch_array($get_data)){
        $final_id = $id['id'];
    }
    print (json_encode(array("Id"=>$final_id)));
}else{
    die("404 SERVER ERROR");
}

&GT;

我的新Javascript .ajax:

    $.ajax({
    type: "POST",
    url: "post_debate.php",
    data: { post_title: title, post_sub: subtitle, post_sides: sides, ajax: 1 },
    dataType: "json",
    timeout: 5000, // in milliseconds
    success: function(data) {
        if(data!==null){
            window.location.replace('show_debate.php?id=' + data['Id']);
        }else{
            window.location.replace('errors/noConnection.html');
        }
    }
});

1 个答案:

答案 0 :(得分:1)

您的代码期待JSON作为回复......

dataType: "json",

Documentation Here

但是,如果没有适当的内容类型标题,则返回非json值。

尝试从

更改PHP脚本
   exit($final_id);

到(未经测试)

header("content-type: application/json");
print (json_encode(array(
        "Id"=>$final_id
    )));

另外,在Javascript代码中使用success回调设置断点(使用Firebug或类似工具)并检查data包含的内容。它现在应该是一个关联数组,所以你可以做

window.location.replace('show_debate.php?id=' + data['Id']);

改进:

使用mysql_insert_id()而不是使用SELECT来获取最近插入的ID。像这样......

$query = mysql_query("INSERT INTO debates VALUES('','$title','$subtitle','$sides','0','0','$timeStamp')");
$final_id = mysql_insert_id();
print (json_encode(array("Id"=>$final_id)));

另外,如果在开发工具中看不到响应,另一种测试PHP返回内容的方法是直接浏览页面(您必须将所有$_POST更改为{ {1}})