gpt4 book ai didi

php - 提交时如何防止粘性复选框重置? (一页多表)

转载 作者:行者123 更新时间:2023-11-29 00:42:30 25 4
gpt4 key购买 nike

我正在使用 Dreamweaver 并在一个页面上有两个表单,每个表单都使用 php 提交到同一个 mySQL 数据库,但行不同。它们每个都有“粘性”代码以防止它们在我将数据提交到 mySQL 时丢失值.问题是当我提交 form2 时 form1 丢失了值,反之亦然。整整两天让我发疯。提前致谢。

<?php require_once('Connections/pmpConn.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}

$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}



$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form2")) {
$updateSQL = sprintf("UPDATE readiness_one_day SET on_time=%s WHERE ready_primary=%s",
GetSQLValueString(isset($_POST['on_time2']) ? "true" : "", "defined","1","0"),
GetSQLValueString($_POST['ready_primary2'], "int"));

mysql_select_db($database_pmpConn, $pmpConn);
$Result1 = mysql_query($updateSQL, $pmpConn) or die(mysql_error());

}

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
$updateSQL = sprintf("UPDATE readiness_one_day SET on_time=%s WHERE ready_primary=%s",
GetSQLValueString(isset($_POST['on_time1']) ? "true" : "", "defined","1","0"),
GetSQLValueString($_POST['ready_primary1'], "int"));

mysql_select_db($database_pmpConn, $pmpConn);
$Result1 = mysql_query($updateSQL, $pmpConn) or die(mysql_error());
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>
<body>

<form id="form1" name="form1" method="POST" action="<?php echo $editFormAction; ?>">
<p>
<input name="ready_primary1" type="text" id="ready_primary1" value="1" size="2" maxlength="2" readonly="readonly" />
</p>
<p>
<input name="on_time1" type="checkbox" id="on_time1" value="1"<?php if (isset($_POST['on_time1']) && ($_POST['on_time1'] == '1')) echo 'checked="checked" '; ?> /> <!code for sticky checkboxes>
<label for="on_time1">On Time</label>
</p>
<p>
<input type="submit" name="submit1" id="submit1" value="Update" />
</p>
<input type="hidden" name="MM_update" value="form1" />
</form>
<form id="form2" name="form2" method="POST" action="<?php echo $editFormAction; ?>" >
<p>
<input name="ready_primary2" type="text" id="ready_primary2" value="2" size="2" maxlength="2" readonly="readonly" />
</p>
<p>
<input name="on_time2" type="checkbox" id="on_time2" value="1" <?php if (isset($_POST['on_time2']) && ($_POST['on_time2'] == '1')) echo 'checked="checked" '; ?> /> <!code for sticky checkboxes>
<label for="on_time2">On Time</label>
</p>
<input type="submit" name="submit2" id="submit2" value="Update" />
<input type="hidden" name="MM_update" value="form2" />
</form>
</body>
</html>

最佳答案

您应该在每个表单提交后立即设置 $_SESSION 变量,这样您就可以在页面之间保留值。之后,您可以执行以下操作:

在表单提交页面上:

<?php 
session_start();
$SESSION['one_box'] = True;
?>

在带有表单的页面上:

<?php 
start_session();
function selected($value) {
if (isset($_SESSION[$value]) {
return "checked='yes'";
}
else {
return "";
}
?>

<input type="checkbox" name="one_box" <?php echo selected("one_box"); ?> />

关于php - 提交时如何防止粘性复选框重置? (一页多表),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11706125/

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