gpt4 book ai didi

javascript - 检查表单更改后无法单击输入

转载 作者:行者123 更新时间:2023-12-03 03:04:58 25 4
gpt4 key购买 nike

这是我在这里发表的第一篇文章,所以请保持温柔。

当用户离开他们所在的页面时,我尝试在表单上执行自动保存。主要适用于用户已经使用记录,然后继续导航到新记录而不首先保存其工作的情况。到目前为止,我已经能够成功检测表单内容是否已更改,但我对如何执行保存感到困惑。

这是我认为可行的代码:

var $formContents = $(document.getElementById("allData"));
origForm = $formContents.serialize();

window.onunload = function() {
var $formContents = $(document.getElementById("allData"));
nowForm = $formContents.serialize();

if (nowForm !== origForm) {
console.log('Changes detected.');
document.getElementById('saveBut').click();
}
else {
console.log('No changes detected');
}
}

现在,数据比较正在进行中。如果我更改了表单中的任何内容,我会在控制台中看到“检测到更改”注释。如果没有,我会收到“未检测到任何更改”。

但是,“document.getElementById('saveBut').click();”没有运行,并且控制台没有显示任何错误。 “saveBut”输入包含在后方法表单中,它会触发 php 代码将表单数据保存到我的 SQL 服务器。

FWIW,这是输入的 html:

<form method="post" id="save">
<input type="submit" form="allData" value="Save Changes" id="saveBut" name="saveBut"/>
</form>

<form method="post" id="allData">
<input type="hidden" name="formData[]" value="<?php echo $row['ID']; ?>">
<div class="info">
Sermon Date: <input type="date" name="formData[]" value="<?php echo $row['sermon_date']; ?>">
Sermon Location: <input type="text" name="formData[]" value="<?php echo $row['sermon_location']; ?>">
Call to Worship: <input type="text" name="formData[]" value="<?php echo $row['call_to_worship']; ?>">
Hymn of Response: <input type= "text" name="formData[]" value="<?php echo $row['hymn_of_response']; ?>">
</div>

<br><hr style="width:90%"><br>

<div class="top">
Pericope:
<input type="text" size="40" name="formData[]" value="<?php echo htmlspecialchars($row['pericope'], ENT_QUOTES); ?>">
//and on and on...
</form>

我也尝试过通过替换直接调用php代码

document.getElementById('saveBut').click();

与:

$.get('saverec.php', function(data) {
eval(data);
});

然后我尝试了:

$.ajax({
type: "GET",
url: "saverec.php"
});

但是两者都不起作用,而且我仍然在控制台日志中得到相同的结果“检测到更改”,但没有错误。我确信我错过了一些非常基本的东西,但我不知道那可能是什么。

有没有办法让我的 saverec.php 代码以这种方式运行,或者我是否需要放弃这种困惑并想出不同的方法?

<小时/>

编辑:

我正在添加 saverec.php 文件中的代码。仅供引用,此代码没有任何问题,因为当用户单击“saveBut”输入时它运行良好。

<?php

$location = "i.p.add.ress";
$username = "my_username";
$password = "my_password";
$dbname = "my_database_name";
$tableName = "my_table_name";
$conn = new mysqli($location, $username, $password, $dbname);

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$allData = $_POST['formData'];
$currentID = $allData[0];

//write the form data to the new record
class updateRecord {
public function __construct ($colName, $data, $currentID) {
$this->colName = $colName;
$this->data = $data;
$this->currentID = $currentID;
}

public function writeDataText() {
global $conn;
$ID = strval($this->currentID);
$tableName = "sermon_prep_database";
$sql = "UPDATE " . $tableName . " SET " . $this->colName . "='" . mysqli_real_escape_string($conn, $this->data) . "' WHERE ID=" . $this->currentID;
mysqli_query($conn, $sql);
}
}

//write all the data to the proper columns
$row = new updateRecord('sermon_date', $allData[1], $currentID);
$row->writeDataText();
$row = new updateRecord('sermon_location', $allData[2], $currentID);
$row->writeDataText();
$row = new updateRecord('call_to_worship', $allData[3], $currentID);
$row->writeDataText();
$row = new updateRecord('hymn_of_response', $allData[4], $currentID);
$row->writeDataText();
$row = new updateRecord('pericope', $allData[5], $currentID);
$row->writeDataText();
$row = new updateRecord('pericope_texts', $allData[6], $currentID);
$row->writeDataText();
$row = new updateRecord('sermon_text', $allData[7], $currentID);
$row->writeDataText();
$row = new updateRecord('fcft', $allData[8], $currentID);
$row->writeDataText();
$row = new updateRecord('gat', $allData[9], $currentID);
$row->writeDataText();
$row = new updateRecord('cpt', $allData[10], $currentID);
$row->writeDataText();
$row = new updateRecord('purpose_bridge', $allData[11], $currentID);
$row->writeDataText();
$row = new updateRecord('fcfs', $allData[12], $currentID);
$row->writeDataText();
$row = new updateRecord('gas', $allData[13], $currentID);
$row->writeDataText();
$row = new updateRecord('cps', $allData[14], $currentID);
$row->writeDataText();
$row = new updateRecord('sermon_title', $allData[15], $currentID);
$row->writeDataText();
$row = new updateRecord('sermon_scripture', $allData[16], $currentID);
$row->writeDataText();
$row = new updateRecord('text_outline', $allData[17], $currentID);
$row->writeDataText();
$row = new updateRecord('research_notes', $allData[18], $currentID);
$row->writeDataText();
$row = new updateRecord('sermon_outline', $allData[19], $currentID);
$row->writeDataText();
$row = new updateRecord('illustrations', $allData[20], $currentID);
$row->writeDataText();
$row = new updateRecord('sermon_manuscript', $allData[21], $currentID);
$row->writeDataText();

echo "<script type='text/javascript'>
alert ('Changes saved to ID ' + $currentID);
</script>";

最佳答案

Ajax 调用应该可以工作,您尝试过吗:

$.ajax({
url: "saverec.php",
success: function(data){
console.log('Changes saved.');
}
});

并且您应该只有一种表单来获取数据:

<form method="post" id="save">
<input type="hidden" name="formData[]" value="<?php echo $row['ID']; ?>">
<div class="info">
Sermon Date: <input type="date" name="formData[]" value="<?php echo $row['sermon_date']; ?>">
Sermon Location: <input type="text" name="formData[]" value="<?php echo $row['sermon_location']; ?>">
Call to Worship: <input type="text" name="formData[]" value="<?php echo $row['call_to_worship']; ?>">
Hymn of Response: <input type= "text" name="formData[]" value="<?php echo $row['hymn_of_response']; ?>">
</div>

<br><hr style="width:90%"><br>

<div class="top">
Pericope:
<input type="text" size="40" name="formData[]" value="<?php echo htmlspecialchars($row['pericope'], ENT_QUOTES); ?>">
//and on and on...
<input type="submit" form="allData" value="Save Changes" id="saveBut" name="saveBut"/>
</form>

关于javascript - 检查表单更改后无法单击输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47212239/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com