语法错误:意外令牌<在对话框中提交表单时

时间:2014-12-18 15:02:13

标签: php jquery ajax

我完全陷入困境。每次我尝试通过对话框提交时,我都会收到错误“语法错误:意外令牌<”。在开发控制台中没有任何错误或至少没有任何显示。 PHP部分似乎工作正常,但我认为可能存在问题,因为jquery没有抛出任何错误。

当用户提交地址更改时,查询会检查是否存在匹配的ID号,然后执行相应的脚本;如果存在更新,否则插入。

的jQuery

$('#ChangeOfAddress').click(function() {
    //change of address dialog
    $( "#ChangeAddressDialog" ).dialog({
        width:500,
        modal:true,
        closeOnEscape:true,
        buttons: [ 
            { text: "Ok", type: "submit", click: function() { 
                    var parcel_id = $('#ParcelId').val();
                    var address_1 = $('#Address1').val();
                    var address_2 = $('#Address2').val();
                    var city = $('#City').val();
                    var state = $('#State').val();
                    var zip = $('#Zip').val();
                    var country = $('#Country').val();
                    $.ajax({
                        url: "classes/add-address.php?parcel_id=" + parcel_id + "&address_1=" + address_1 + "&address_2=" + address_2 + "&city=" + city + "&state=" + state + "&zip=" + zip + "&country=" + country,
                        type: "GET",
                        data: { parcel_id : parcel_id, address_1 : address_1, address_2 : address_2, city : city, state : state, zip : zip, country : country },
                        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");
                            alert("Change of address has been submitted!");
                        }
                    });
                } 
            },
            { text: "Close", click: function() { $(this).dialog( "close" ); } } ]
    });//end dialog
});//end change of address click function

PHP:

<?php

require_once('../config.php');

$parcel_id = isset($_GET['parcel_id']) ? $_GET['parcel_id'] : null;
$address1 = isset($_GET['address_1']) ? $_GET['address_1'] : null;
$address2 = isset($_GET['address_2']) ? $_GET['address_2'] : null;
$city = isset($_GET['city']) ? $_GET['city'] : null;
$state = isset($_GET['state']) ? $_GET['state'] : null;
$zip = isset($_GET['zip']) ? $_GET['zip'] : null;
$country = isset($_GET['country']) ? $_GET['country'] : null;

if(isset($_GET['parcel_id'])) {
    $db = new ezSQL_mysql(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
    $parcelExists = $db->get_var("select count(*) as count from change_of_address where parcel_id=" . $_GET['parcel_id']);
    echo $parcelExists;
    $db->debug();
    if ($parcelExists == 1){
        //update db information
        $result = $db->query("UPDATE change_of_address 
            SET address_1='" . $address1 . "', 
                address_2='" . $address2 . "', 
                city='" . $city . "', 
                state='" . $state . "', 
                zip='" . $zip . "', 
                country='" . $country . "'
            WHERE parcel_id=" . $parcel_id);
        $db->debug();
        if ($result == TRUE) {
            echo '{"success":true}';
        } else {
            echo '{"success":false}';
        }
        $db->debug();
    } else {
        //insert into db
        $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 . "')");
        $db->debug();
        if ($result == TRUE) {
            echo '{"success":true}';
        } else {
            echo '{"success":false}';
        }
        $db->debug();
    }
}

?>

1 个答案:

答案 0 :(得分:0)

您需要在“网络”标签中查看AJAX结果。也许您的PHP脚本返回404错误或数据库错误等,并且json无法解析结果,因此会抛出此错误。