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

标签 php javascript mysql html

我正在使用 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/

相关文章:

javascript - 如何将两点之间的线从曲线更改为具有一个 Angular 点的直线

javascript - 如何在 iFrame body 标记上设置 jQuery data() 并从 iFrame 内部检索它?

javascript - iPad Safari - 文本选择事件

mysql - 开发显示查询

mysql - 存储 wreSTLers、他们的角色和他们的入口音乐之间的关系的最合乎逻辑的方法是什么?

mysql - 使用 CONCAT 的 SQL 语法

php - 如何设计一个只捕获没有特定字符组合的字符串的模式?

php - 为什么静态调用这个方法有效?

javascript - 如何使用 ajax 请求 javascript/PHP 在远程服务器上存储 session

php - 如何正确地将表单数据传递到CodeIgniter中的数据库函数