Ajax JSON PHP Stringify未发布但成功返回

Ajax JSON PHP Stringify未发布但成功返回,php,ajax,json,stringify,Php,Ajax,Json,Stringify,我有一个ajax脚本,它从输入中收集值,然后使用MYSQLI插入mysql数据库 单击按钮时,将成功返回唯一id,但不会将任何post值插入数据库。此外,当单独测试post值时,它们也不会显示为达到发布的php脚本 运行此操作后,数据库中会出现唯一id,但不会存储其他信息 Q.为什么变量没有到达PHP,从而阻止我成功插入数据库? 阿贾克斯 $('#postData').click(function() { smallVan_getForm_values(); $.aja

我有一个ajax脚本,它从输入中收集值,然后使用
MYSQLI
插入mysql数据库

单击按钮时,将成功返回唯一id,但不会将任何post值插入数据库。此外,当单独测试post值时,它们也不会显示为达到发布的php脚本

运行此操作后,数据库中会出现唯一id,但不会存储其他信息

Q.为什么变量没有到达PHP,从而阻止我成功插入数据库?

阿贾克斯

$('#postData').click(function() {
        smallVan_getForm_values();

    $.ajax({
     type: "POST",
     dataType: "json",
     url: "//xxxxx",
     data: JSON.stringify({
            book_email:book_email,
            book_clientTitle : book_clientTitle,
            book_firstname : book_firstname,
            book_lastname :book_lastname,
            book_dateofbirth : book_dateofbirth,
            book_primarycontactnumber : book_primarycontactnumber,
            book_secondarycontactnumber : book_secondarycontactnumber,
            departing_first_lineaddress : departing_first_lineaddress,
            departing_second_lineaddress : departing_second_lineaddress,
            departing_postcode : departing_postcode,
            destination_first_lineaddress : destination_first_lineaddress,
            destination_second_lineaddress : destination_second_lineaddress,
            destination_postcode : destination_postcode,
            amount : amount,
            book_consignmentdate : book_consignmentdate,
            book_timeofbooking : book_timeofbooking,
            book_additionalnotes : book_additionalnotes
    }),
     contentType: "application/json; charset=utf-8",

        success: function (result) {
        //  alert(result);

            if (result.status == "success"){
                    uniqueID = result.uniqueID;
                    alert(uniqueID);

            }

       }
    });

});

});
//AJAX END
PHP

if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')
{ //if request made from localserver then proceed here
    insertData();
}


function insertData() {

$uniqueID = substr(str_shuffle(str_repeat("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWYXZ", 10)), 0, 10);


    $conn=mysqli_connect("xxxx","xxx","xxxxx^","xxxxx");
    // Check connection
    if (mysqli_connect_errno())
      {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }

    // Perform queries
    mysqli_query($conn,"INSERT INTO bookings_table ( booking_uniqueID , book_email , book_clientTitle , book_firstname , book_lastname , book_dateofbirth , book_primarycontactnumber , book_secondarycontactnumber , departing_first_lineaddress , departing_second_lineaddress , departing_postcode , destination_first_lineaddress , destination_second_lineaddress , destination_postcode , amount , book_consignmentdate , book_timeofbooking , book_additionalnotes )
   VALUES ( '$uniqueID' , '$_POST[book_email]' , '$_POST[book_clientTitle]' , '$_POST[book_firstname]' , '$_POST[book_lastname]' , '$_POST[book_dateofbirth]' , '$_POST[book_primarycontactnumber]' , '$_POST[book_secondarycontactnumber]' , '$_POST[departing_first_lineaddress]' , '$_POST[departing_second_lineaddress]' , '$_POST[departing_postcode]' , '$_POST[destination_first_lineaddress]' , '$_POST[destination_second_lineaddress]' , '$_POST[destination_postcode]' , '$_POST[amount]' , '$_POST[book_consignmentdate]' , '$_POST[book_timeofbooking]' , '$_POST[book_additionalnotes]' )");

    mysqli_close($conn);
    echo json_encode(array('status' => 'success', 'uniqueID' => $uniqueID));

}

只需删除JSON.stringify()


如果您要返回
uniqueID
,那么显然会调用
mysqli\u query
mysqli\u query
返回的值是多少?我正在返回uniqueID是的。但是发布的数据没有被插入,因为它似乎没有发布到sql。sql只插入唯一ID,因为它看起来都在那里。通过在php页面上发布VAR来测试这一点,它们没有通过ajax到达php页面。对不起,我误解了您的问题。很高兴看到其他人理解了这一点!我已经试过了,但它仍然没有插入任何内容。当我用一个简单的POST ajax测试它时,它们都返回了正确的结果,当我开始使用json时,它们没有返回正确的结果。我只是用一些POST参数尝试了你的代码,然后删除了
json.stringify()
contentType:“application/json;charset=utf-8”
,它起作用了。您的数据是用JSON格式化的,您的参数数据类型之前设置为
JSON
。contentType的默认编码是
charset=utf-8
,所以我删除了它。我错过了取出
contentType:“application/json;charset=utf-8”
它现在可以工作了,好的,非常感谢!
data: {
            book_email:book_email,
            book_clientTitle : book_clientTitle,
            book_firstname : book_firstname,
            book_lastname :book_lastname,
            book_dateofbirth : book_dateofbirth,
            book_primarycontactnumber : book_primarycontactnumber,
            book_secondarycontactnumber : book_secondarycontactnumber,
            departing_first_lineaddress : departing_first_lineaddress,
            departing_second_lineaddress : departing_second_lineaddress,
            departing_postcode : departing_postcode,
            destination_first_lineaddress : destination_first_lineaddress,
            destination_second_lineaddress : destination_second_lineaddress,
            destination_postcode : destination_postcode,
            amount : amount,
            book_consignmentdate : book_consignmentdate,
            book_timeofbooking : book_timeofbooking,
            book_additionalnotes : book_additionalnotes
    },