我有几个我在下面编写的脚本,它们单独工作得很好,但它们一起不能正常工作。让我发布代码,然后解释问题:
自动保存功能:
<script>
function autosave() {
$('form').each(function() {
var string = $(this).closest('form').serialize();
var $this = $(this);
$.ajax({
type: "POST",
url: "add_room.php",
data: string,
cache: false,
success: function(data){
var saveIcon = $this.siblings('.saveIcon');
$this.siblings('[type=hidden]').val(data);
saveIcon.fadeIn(750);
saveIcon.delay(500).fadeOut(750);
$('#message').text('The id of the inserted information is ' + data);
}
});
});
}
setInterval(autosave, 10 * 1000);
</script>
AJAX 发布和返回脚本:
<script>
$(document).ready(function() {
$('body').on('click', '.save', function(e) {
var string = $(this).closest('form').serialize();
var $this = $(this);
$.ajax({
type: "POST",
url: "add_room.php",
data: string,
cache: false,
success: function(data){
var saveIcon = $this.siblings('.saveIcon');
$this.siblings('[type=hidden]').val(data);
saveIcon.fadeIn(750);
saveIcon.delay(500).fadeOut(750);
$('#message').text('The id of the inserted information is ' + data);
}
});
});
});
$(document).ready(function(){
$('#addForm').on('click', function(){
$('<form method="post" action="add_room.php"><label for="itemName[]">Item</label><input type="text" name="itemName[]"><label for="itemPhoto[]">Photo</label><input type="text" name="itemPhoto[]"><input type="hidden" name="itemId[]" value=""><input type="hidden" name="itemParent[]" value="<?=$_GET["room"]?>"><div class="saveIcon" style="display: none; color: green;">SAVED!</div><div class="save">Save Item</div></form>').fadeIn(500).appendTo('.addItem');
});
});
</script>
形式:
<form method="post" action="add_room.php">
<label for="itemName[]">Item</label>
<input type="text" name="itemName[]">
<label for="itemPhoto[]">Item</label>
<input type="text" name="itemPhoto[]">
<input type="hidden" name="itemId[]" value="">
<input type="hidden" name="itemParent[]" value="<?=$_GET['room']?>">
<div class="saveIcon" style="display: none; color: green;">SAVED!</div>
<div class="save">Save Item</div>
</form>
PHP:
<?PHP
require_once('dbConfig.php');
$item = $_POST['itemName'];
$photo = $_POST['itemPhoto'];
$id = $_POST['itemId'];
$parentId = $_POST['itemParent'];
foreach($item as $key => $val) {
if(!$id[$key]) {
if ($stmt = $db->prepare("INSERT test (test_title, test_desc, test_parent) VALUES (?, ?, ?)"))
{
// Use an s per variable passed to the string, example - "ss", $firstname, $lastname
$stmt->bind_param("ssi", $val, $photo[$key], $parentId[$key]);
$stmt->execute();
$stmt->close();
echo $db->insert_id;
//echo "success";
}
// show an error if the query has an error
else
{
echo "ERROR: Could not prepare Insert SQL statement.";
}
}
else
{
if ($stmt = $db->prepare("UPDATE test SET test_title = ?, test_desc = ? WHERE test_id = ?"))
{
// Use an s per variable passed to the string, example - "ss", $firstname, $lastname
$stmt->bind_param("ssi", $val, $photo[$key], $id[$key]);
$stmt->execute();
$stmt->close();
echo $id[$key];
//echo "success";
}
// show an error if the query has an error
else
{
echo "ERROR: Could not prepare Update SQL statement.";
}
}
}
?>
现在,第二个脚本发生的情况是,当您在没有自动保存的情况下单独使用时,当您填写表单并单击保存时,它会获取表单数据并将其保存到数据库中的必要行,然后返回 id刚刚保存的内容并将该数据放在隐藏字段中,以便 php 脚本可以解决是否需要插入查询或更新查询(当返回的 id 存在时)。还有一个名为 addForm 的可点击 div,然后将另一个表单集附加到存在的表单下方,然后再次单击它的保存按钮时,仅在数据库中保存/更新此表单。当我像在我的代码中那样触发自动保存时,自动保存从字面上获取所有表单并将它们保存为新条目,但不返回 id/更新隐藏字段以触发更新序列。你能对此有所了解吗?这真的让我很烦。已尽我所能解释这一点,对不起,它太长了。这有点复杂!哈哈