【问题标题】:Ajax JSON PHP Stringify not posting but returning successfullyAjax JSON PHP Stringify未发布但成功返回
【发布时间】:2015-04-26 21:40:59
【问题描述】:

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

当我单击按钮时,唯一 ID 成功返回,但没有任何帖子值被插入到数据库中。此外,在单独测试帖子值时,它们也不会显示为到达发布的 php 脚本。

运行此操作后,唯一 ID 在数据库中,但没有存储其他信息。

Q. How come the variables not reaching the PHP, thus preventing me from a successful insertion to the database?

阿贾克斯

$('#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));

}

【问题讨论】:

  • 如果你得到 uniqueID 回来,那么显然 mysqli_query 正在被调用。 mysqli_query 返回什么值?
  • 我得到了 uniqueID 是的。但是发布的数据没有被插入,因为它似乎没有发布到 sql。 sql 只插入唯一ID,因为它似乎都在那里。通过在 php 页面上发布 var 对此进行了测试,它们没有通过该 ajax 到达 php 页面。
  • 对不起,我误解了你的问题。很高兴看到其他人理解它!

标签: php ajax json stringify


【解决方案1】:

只需删除您的 JSON.stringify()。

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
    },

【讨论】:

  • 我试过了,它仍然没有插入任何东西。
  • 当我用一个简单的 POST ajax 测试它时,它们都正确返回,当我开始使用它们不使用的 json 时。
  • 我只是用一些 post 参数尝试你的代码,然后我删除了 JSON.stringify()contentType: "application/json; charset=utf-8",它起作用了。您的数据格式为 JSON,您的参数 dataType 之前设置为 JSON。并且 contentType 默认编码是charset=utf-8,所以我删除它。
  • 我错过了contentType: "application/json; charset=utf-8" 现在可以使用了,好的,非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-08-17
  • 2021-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多