数据未插入数据库

时间:2014-12-03 19:34:44

标签: php jquery ajax

所以我认为jquery ajax的序列化部分存在问题。它没有将任何信息放入数据库,我不明白为什么!显然,网页上输入控件的变量没有传递到php处理页面。我在这做错了什么?在这里需要一些帮助!这对我来说都很新鲜。

网页:

 <form name="main_form" id="main_form" method="post">
    <div id="ChangeAddressDialog" title="Change of Address">
        <p>Mailing Address: <input type="text" id="Address1" name="Address1" /></p>
        <p>Mailing Address 2: <input type="text" id="Address2" name="Address2" /></p>
        <p>City: <input type="text" id="City" name="City" /></p>
        <p>State: <input type="text" id="State" name="State" maxlength="2" /></p>
        <p>Zip Code: <input type="text" id="Zip" id="Zip" maxlength="10" /></p>
        <p>Country: <input type="text" id="County" name="Country" /></p>
        <input type="hidden" id="change_of_address_form" name="change_of_address_form" />
    </div>
  </form>

    $('#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处理页面:

<?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 '{"success":true}';
} else {
    echo '{"success":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 . "')";

//echo json_encode($sqlCheck);


?>

1 个答案:

答案 0 :(得分:1)

data: $("#main_form").serialize(),

表单元素名为 main_form。您的jquery选择器正在寻找 id 。您只需在表单标记上将name="main_form"更改为id="main_form"即可修复它。