//Check boxes Function
$(function() {
//If check_all checked then check all table rows
$("#check_all").on("click", function () {
if ($("input:checkbox").prop("checked")) {
$("input:checkbox[name='row-check']").prop("checked", true);
} else {
$("input:checkbox[name='row-check']").prop("checked", false);
}
});
// Check each table row checkbox
$("input:checkbox[name='row-check']").on("change", function () {
var allchkbx = $("input:checkbox[name='row-check']").length;
var ttchkbx = $("input:checkbox[name='row-check']:checked").length;
// If all checked manually then check check_all checkbox
if (allchkbx === ttchkbx) {
$("#check_all").prop("checked", true);
}
else {
$("#check_all").prop("checked", false);
}
});
$("#sendall").on('click', function() {
var array = [];
$("input:checked").each(function() {
array.push($(this).val());
});
// console.log(array); //debug log
$('#sendall_Modal').modal('show');
$('#sendall_form').on("submit", function(event){
event.preventDefault();
var dbarray = JSON.stringify(array);
// console.log(dbarray);
$.ajax({
url:"../../bl/office/sendall.php",
method:"POST",
data:{dbarray:dbarray},
cache:false,
success:function(data){
$('#sendall_form')[0].reset();
$('#sendall_Modal').modal('hide');
},
});
});
});
});
我正在使用此代碼將訊息 ID 傳遞給一個 php 腳本,然后該腳本通過選擇帶有傳遞的 ID 的訊息將 SMS 發送到客戶端電話號碼。當我使用 check all 并重新加載我的頁面時,我的代碼作業正常。但是,如果我在不重新加載的情況下從同一個表中選擇記錄,上面的代碼將首先將先前選擇的記錄傳遞給 php 腳本,然后傳遞新選擇的記錄。
當我單擊發送并檢查所有記錄時,我的開發工具將向 sendall.php 腳本顯示一個請求。
如果我取消選中所有并選中一些框,開發工具將顯示兩個請求,第一個包含所有記錄,第二個包含較少的記錄。
取消選中后如何確保只有一個對 sendall.php 的請求?
這是我的 sendall.php 腳本
$data = json_decode(stripslashes($_POST['dbarray']));
if ($data[0] == "on"){
foreach(array_slice($data,1) as $d){
$query = "SELECT msisdn, message FROM off_texts where id=$d";
$result = mysqli_query($conn, $query);
$rows = mysqli_fetch_assoc($result);
$phoneno = $rows['msisdn'];
$text = $rows['message'];
}
}
else {
foreach(array_slice($data,0) as $d){
$query = "SELECT msisdn, message FROM off_texts where id=$d";
$result = mysqli_query($conn, $query);
$rows = mysqli_fetch_assoc($result);
$phoneno = $rows['msisdn'];
$text = $rows['message'];
}
};
這是我的模態 html
<div id="sendall_Modal" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="modal-title"></h5>
<button type="button" class="btn-close" data-dismiss="modal" data-bs-dismiss="modal" aria-label="Close"></button>
<h5 class="modal-title" id="modal-title"></h5>
</div>
<div class="modal-body">
<form id="sendall_form">
<label>Confirm:</label>
<p> Are you sure you want to send these Messages? </p>
<br />
<input type="submit" name="ainsert" id="ainsert" value="Send" class="btn btn-success" />
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" data-dismiss="modal" data-bs-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
uj5u.com熱心網友回復:
你已經把$('#sendall_form').on("submit", function(event){ 里面 $("#sendall").on('click', function() {。這意味著每次用戶單擊“sendall”時,它都會注冊另一個“提交”事件處理程式。它不會洗掉以前的。因此,如果有人單擊 sendall 兩次,那么下次提交表單時,它將發送兩個 AJAX 請求(因為事件處理程式注冊了兩次)。
據我所知,您的“發送”按鈕可能只需要打開模態。所有其他處理都可以在提交表單時進行。宣告“提交”事件處理程式的代碼應該在“點擊”處理程式之外,這樣它只會系結到表單一次。
像這樣的東西:
$("#sendall").on('click', function() {
// console.log(array); //debug log
$('#sendall_Modal').modal('show');
});
$('#sendall_form').on("submit", function(event){
event.preventDefault();
var array = [];
$("input:checked").each(function() {
array.push($(this).val());
});
var dbarray = JSON.stringify(array);
// console.log(dbarray);
$.ajax({
url:"../../bl/office/sendall.php",
method:"POST",
data:{dbarray:dbarray},
cache:false,
success:function(data){
$('#sendall_form')[0].reset();
$('#sendall_Modal').modal('hide');
},
});
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/370777.html
