[英]Having issues saving the dynamic state of a checkbox (Javascript, jQuery)
I'm trying to make a form where it allows you to proceed to another webpage if a checkbox has been checked, and prompts you with an alert otherwise. 我正在尝试制作一个表格,如果已选中一个复选框,则该表格可让您进入另一个网页,否则将向您发出警报。
My issue is that the checkbox won't save the state dynamically. 我的问题是该复选框不会动态保存状态。 As a result, I have to refresh the page everytime I check the checkbox in order to proceed to the next webpage. 结果,每次检查该复选框时,我都必须刷新页面才能进入下一个网页。 Is there something I need to add to my code? 我需要添加一些代码吗? I've attached the javascript function, the PHP page that changes the state in the SQL, as well as the line where I call the function itself. 我已经附加了javascript函数,用于更改SQL状态的PHP页面以及调用函数本身的行。
Javascript function: JavaScript函数:
//This function handles the clicking of checkboxes in the list
//It sends an ajax POST message to a separate php file that handles the database updating
function checkboxClicked(checkboxType, ticketNumber){
$.ajax({
type: "POST",
url: 'updateCheckbox.php',
dataType: 'json',
data: {checkboxType: checkboxType, ticketNumber: ticketNumber},
success: function(result, status){
if (status != "success"){
console.log("Status: " + status + ", Result: " + result);
}
}
});
}
PHP file (updatecheckbox.php) PHP文件(updatecheckbox.php)
<?php
header('Content-Type: application/json');
$conn = new COM("ADODB.Connection");
$strCnn = "Provider=sqloledb; Data Source=booty;Initial Catalog=booty;Trusted_Connection=Yes";
$conn->open($strCnn);
$strSQL = "UPDATE tblScantronTickets SET ".$_POST['checkboxType']."=";
$strSQL .= "(SELECT ~".$_POST['checkboxType']." FROM tblScantronTickets WHERE TicketNumber=".$_POST['ticketNumber'].")";
$strSQL .= " WHERE TicketNumber=".$_POST['ticketNumber'];
try {
$conn->Execute($strSQL);
}
catch(Exception $e) {
echo json_encode($e);
}
echo json_encode("SUCCESS!");
?>
PHP line where I call the function: 我在其中调用函数的PHP行:
$strHTML .= "<td align='center'> <input type='checkbox' ".$received." class='receivedCheckbox' onchange='checkboxClicked(\"Received\", ".$ticketNumber.")'/> </td>\n";
If your problem is to get the state of a checkbox before proceed to the next page, you can use jQuery. 如果您的问题是在进入下一页之前获取复选框的状态,则可以使用jQuery。
$('#checkbox').is(':checked');
This will return true if it is checked and false if it is not 如果选中则返回true,否则返回false。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.