所以我有这个表格:
echo '<form id="target" action="edit-property.php?Property='.$property.'" method="post">';
echo "Address: ".'<input type="text" name="address" value="'.$propAddress.'" />';
echo '<input type="submit" value="Update">';
echo "</form>";在提交时,我使用此函数更改数据库条目(我仍在学习从mysql到mysqli的更改,所以我不确定我是否做得正确,并且我不完全知道如何在更新表时绑定params,但这不是我的问题)
$property = $_GET['Property']
if (isset($_POST['address']) && !empty($_POST['address'])) {
$mysqli = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME);
if (!$mysqli) {
die('There was a problem connecting to the database.');
}
else {
$address = mysqli_real_escape_string($mysqli, $_POST['address']);
$id = intval($property);
if($id) {
$query =("UPDATE Properties
SET Address = '$address'
WHERE Id = '$id'");
if (mysql_query($query)){
echo '1';
exit;
}
else {
echo '0';
exit;
}
$mysqli->close();
}
}
}好的。所以效果还可以(如果我在这里做了什么愚蠢的事情,请随时纠正我,我发现我从批评中学到了最好的东西,stackoverflow帮助我提高了很多!)
但这是我的两难境地。我想在不刷新页面的情况下更新页面。
我偶然发现下面的代码,修改它以适合我,不幸的是它不能工作。恐怕我不知道为什么:
<script>
var id = <?=$id?>;
// when the DOM is ready
$(document).ready(function() {
$('#target').submit(function(e) {
// prevent normal, boring, tedious form submission
e.preventDefault();
// send it to the server out-of-band with XHR
$.post('edit-property.php?Property=' + id, function() {
data: $(this).val(),
success: function(resp) {
if(resp == '1') {
alert('Saved successfully');
} else {
alert('Oops, something went wrong!');
}
}
});
});
});
</script>不幸的是,这是一个谜--不能让它工作。我能做错什么呢?
谢谢您:)
发布于 2013-02-03 17:28:01
我认为您误解了此调用的语法,并将一些用于$.post的参数包装到一个多余的“函数”中,您可能希望使用它来完成调用。
$.post('edit-property.php?Property=' + id,
/*function() {*/
data: $(this).val(),
success: function(resp) {
if(resp == '1') {
alert('Saved successfully');
} else {
alert('Oops, something went wrong!');
}
/*}*/
});https://stackoverflow.com/questions/14670906
复制相似问题