我不知道我做错了什么。我有一个对话框,用户可以输入地址并单击确定,然后将输入的信息插入到数据库中。我不断收到“意外的输入结束”错误或“意外令牌”错误。我觉得它与jquery ajax函数中的serialize属性有关,但我不确定。
我不知道如何回显变量以查看它们是否甚至从字段或sql语句中获取数据。我已经尝试了很多,它或者崩溃了页面,或者它给了我同样的“意外”错误。
请帮忙! :(
jQuery代码:
$('#ChangeOfAddress').click(function() {
//change of address dialog
$( "#ChangeAddressDialog" ).dialog({
width:500,
modal:true,
closeOnEscape:true,
buttons: [
{ text: "Ok", type: "submit", click: function() {
$.ajax({
url: "classes/add-address.php",
type: "POST",
data: $("#main_form").serialize(),
dataType: 'json',
error: function(SMLHttpRequest, textStatus, errorThrown){
alert("An error has occurred making the request: " + errorThrown)
},
success: function(result){
//do stuff here on success such as modal info
//$("#main_form").submit();
$(this).dialog("close");
}
})
}
},
{ text: "Close", click: function() { $(this).dialog( "close" ); } } ]
});//end dialog
});
PHP页面:( ez sql insert返回bool值)
<?php
require_once('../config.php');
$sqlCheck = '';
$parcel_id = isset($_POST['ParcelId']) ? $_POST['ParcelId'] : null;
$address1 = isset($_POST['Address1']) ? $_POST['Address1'] : null;
$address2 = isset($_POST['Address2']) ? $_POST['Address2'] : null;
$city = isset($_POST['City']) ? $_POST['City'] : null;
$state = isset($_POST['State']) ? $_POST['State'] : null;
$zip = isset($_POST['Zip']) ? $_POST['Zip'] : null;
$country = isset($_POST['Country']) ? $_POST['Country'] : null;
$db = new ezSQL_mysql(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
$result = $db->query("INSERT INTO change_of_address (parcel_id, address_1, address_2, City, State, Zip, Country) VALUES ('" . $parcel_id . "','" . $address1 . "','" . $address2 . "','" . $city . "','" . $state . "','" . $zip . "','" . $country . "')");
if ($result == 1) {
echo true;
} else {
echo false;
}
//$sqlCheck = "INSERT INTO change_of_address (parcel_id, address_1, address_2, City, State, Zip, Country) VALUES ('" . $parcel_id . "','" . $address1 . "','" . $address2 . "','" . $city . "','" . $state . "','" . $zip . "','" . $country . "')";
?>
我尝试添加以下内容,但它没有返回任何错误:
error_reporting(E_ALL);
ini_set('display_errors', 'On');
答案 0 :(得分:2)
您的问题是您的$.ajax
调用期待JSON响应,但您的PHP文件正在回显文字true
或false
。这些不是JSON响应。您需要返回有效的JSON,例如
if ($result == 1) {
echo '{"success":true}';
} else {
echo '{"success":false}';
}