所以我在考慮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);
?>
**警告**:這看起來[恐怖不安全](http://bobby-tables.com/),爲了您的利益,我希望這不在公共互聯網上。您需要確保任何和所有用戶參數[妥善轉義](http://bobby-tables.com/php),否則您將面臨嚴重的應用程序危害風險。只要有可能,請使用準備好的語句和佔位符,以確保您不會遇到此類錯誤。 ['ezSQL_mysql'](http://docs.phpvms.net/internals/d5/d95/classez_s_q_l__mysql.html)確實有一個'escape'功能,你**迫切需要**使用。 – tadman 2014-12-03 19:37:04
@JonathanKuhn ooo謝謝你錯過了!我將它添加到表單標籤,但仍然沒有插入數據庫:/ – maryjane 2014-12-03 19:46:59
@tadman沒有其內部。我需要得到它的功能,然後解決安全問題。老闆只是想把它拿出來,所以他們可以「玩」它 – maryjane 2014-12-03 19:50:37