如何在提交时保持粘贴复选框不被重置? (一页上有多个表格)

时间:2012-07-29 02:16:24

标签: php javascript mysql html

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

<?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>

1 个答案:

答案 0 :(得分:1)

您应该在每个表单提交后立即设置$_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"); ?> />