在Dreamweaver表单中使用多个复选框

时间:2012-06-07 21:13:08

标签: php mysql dreamweaver

我有一个在Adobe Dreamweaver中构建的表单。它可以工作,但是当选中复选框时,最后一个复选框值将提交给数据库。

Dreamweaver中是否有任何选项可以让表单使用所有复选框?

<?php require_once('Connections/MySQL_CSFTDB.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_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO articles (articletitle, articledescription) VALUES     (%s, %s)",
                       GetSQLValueString($_POST['articletitle'], "text"),
                       GetSQLValueString($_POST['articledescription'], "text"));

  mysql_select_db($database_MySQL_CSFTDB, $MySQL_CSFTDB);
  $Result1 = mysql_query($insertSQL, $MySQL_CSFTDB) 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>
<link href="twoColLiqLtHdr.css" rel="stylesheet" type="text/css" /><!--[if lte IE 7]>
<style>
.content { margin-right: -1px; } /* this 1px negative margin can be placed on any of the     columns in this layout with the same corrective effect. */
ul.nav a { zoom: 1; }  /* the zoom property gives IE the hasLayout trigger it needs to     correct extra whiltespace between the links */
</style>
<![endif]-->
</head>

<body>

<div class="container">
  <div class="header"><!-- end .header --></div>
  <div class="sidebar1">
     <ul class="nav">
      <li><a href="index.php">Home</a></li>
      <li><a href="#">Search</a></li>
      <li><a href="addnew.php">Add New Article</a></li>
      <li><a href="addnew.php">Article Admin.</a></li>
      <li><a href="#">Tag Admin.</a></li>
    </ul>
    <p><!-- end .sidebar1 --></p>
</div>
  <div class="content">
    <h1>Article Database</h1>
    <p>- Add a new article.</p>
    <form action="<?php echo $editFormAction; ?>" id="form1" name="form1" method="POST">
      <table width="100%" border="0" cellpadding="6">
        <tr>
          <td width="29%" align="right"><label for="articletitle">Article Title:</label>    </td>
          <td width="71%" align="left"><input name="articletitle" type="text" id="articletitle" size="50" /></td>
        </tr>
        <tr>
          <td align="right"><label for="articleorganization">Article or Organization:</label></td>
          <td align="left"><input name="articleorganization" type="text" id="articleorganization" size="50" /></td>
        </tr>
        <tr>
          <td align="right"><label for="articledate">Access Date:</label></td>
          <td align="left"><input name="articledate" type="text" id="articledate" size="50" /></td>
        </tr>
        <tr>
          <td align="right"><label for="articledescription">Article Description:</label></td>
          <td align="left"><input name="articledescription" type="text" id="articledescription" size="50" /></td>
        </tr>
        <tr>
          <td colspan="2" align="center" valign="bottom"><p>Article Tags</p></td>
        </tr>
        <tr>
          <td align="right">&nbsp;</td>

        <br />
        <label>
          <input type="checkbox" name="CheckboxGroup1" value="option 1" id="CheckboxGroup1_0" />
          Checkbox 1</label>
        <br />
        <label>
          <input type="checkbox" name="CheckboxGroup1" value="option 2" id="CheckboxGroup1_1" />
          Checkbox 2</label>
          <td align="left"><p>
            <br />
            <br />
          </p></td>
        </tr>
        <tr>
          <td align="right">&nbsp;</td>
          <td align="left"><p><br />
          </p></td>
        </tr>
        <tr>
          <td align="right">&nbsp;</td>
          <td align="left">&nbsp;</td>
        </tr>
        <tr>
          <td colspan="2" align="center" valign="middle"><input type="submit" name="submit" id="submit" value="Submit" /></td>
        </tr>
      </table>
      <input type="hidden" name="MM_insert" value="form1" />
    </form>
  <!-- end .content --></div>
  <div class="footer">

  </div>
  <!-- end .container --></div>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

<input type="checkbox" name="CheckboxGroup1" />
<input type="checkbox" name="CheckboxGroup1" />

变为

<input type="checkbox" name="CheckboxGroup1[]" />
<input type="checkbox" name="CheckboxGroup1[]" />

然后你可以读取这样的值:

$selected = array();
if (isset($_POST['CheckboxGroup1']) && is_array($_POST['CheckboxGroup1'])) {
    $selected = $_POST['CheckboxGroup1'];
} else {
    $selected = array($_POST['CheckboxGroup1']);
}

这确保$ selected变量始终是一个数组。要确定是否检查了某个值:

$checked = in_array('value', $selected);