0

我有一个包含三个部分的表格;级别 1、级别 2 和级别 3。每个部分都包含四个文本区域和一个单选组,每个文本区域旁边都有单选按钮:

  <form action="" method="post" name="form" id="form" onSubmit="return validate();">
    <table width="123%" border="0" cellpadding="5">
      <th colspan="6">
        <div align="center"><strong>Level 1</strong></div>      </th>
      <tr>
        <td valign="top"><strong>Pathway</strong></td>
        <td colspan="2"><strong>Blocking or Accepting Comment</strong></td>
        <td valign="top"><strong>Appropriate</strong></td>
      </tr>
      <tr>
        <td width="26%" valign="top"><strong>Pathway 1:</strong> No action - See again</td>
        <td colspan="2"><div class="required">
            <textarea name="l1_p1_comment" id="l1_p1_comment" cols="35" rows="5"></textarea>
          </div></td>
        <td width="18%" valign="top"><p>
            <input type="radio" name="l1_allowed" value="1" id="l1_allowed_0" />
        </td>
      </tr>
      <tr>
        <td valign="top"><strong>Pathway 2:</strong> See again</td>
        <td colspan="2"><div class="required">
            <textarea name="l1_p2_comment" id="l1_p2_comment" cols="35" rows="5"></textarea>
          </div></td>
        <td valign="top"><input type="radio" name="l1_allowed" value="1" id="l1_allowed_1" /></td>
      </tr>
      <tr>
        <td valign="top"><strong>Pathway 3:</strong> Refer to ED</td>
        <td colspan="2"><div class="required">
            <textarea name="l1_p3_comment" id="l1_p3_comment" cols="35" rows="5"></textarea>
          </div></td>
        <td valign="top"><input type="radio" name="l1_allowed" value="1" id="l1_allowed_2" /></td>
      </tr>
      <tr>
        <td valign="top"><strong>Pathway 4:</strong> Refer to Specialist</td>
        <td colspan="2"><div class="required">
            <textarea name="l1_p4_comment" id="l1_p4_comment" cols="35" rows="5"></textarea>
          </div></td>
        <td valign="top"><input type="radio" name="l1_allowed" value="1" id="l1_allowed_3" /></td>
      </tr>
      <tr>
        <td valign="top">&nbsp;</td>
        <td colspan="2">&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
    <th colspan="6">
        <div align="center"><strong>Level 2</strong></div>      </th>
      <tr>
        <td valign="top"><strong>Pathway</strong></td>
        <td colspan="2"><strong>Blocking or Accepting Comment</strong></td>
        <td valign="top"><strong>Appropriate</strong></td>
      </tr>
      <tr>
        <td width="26%" valign="top"><strong>Pathway 1:</strong> No action - See again</td>
        <td colspan="2"><div class="required">
            <textarea name="l2_p1_comment" id="l2_p1_comment" cols="35" rows="5"></textarea>
          </div></td>
        <td width="18%" valign="top"><p>
            <input type="radio" name="l2_allowed" value="1" id="l2_allowed_0" />
        </td>
      </tr>
      <tr>
        <td valign="top"><strong>Pathway 2:</strong> See again</td>
        <td colspan="2"><div class="required">
            <textarea name="l2_p2_comment" id="l2_p2_comment" cols="35" rows="5"></textarea>
          </div></td>
        <td valign="top"><input type="radio" name="l2_allowed" value="1" id="l2_allowed_1" /></td>
      </tr>
      <tr>
        <td valign="top"><strong>Pathway 3:</strong> Refer to ED</td>
        <td colspan="2"><div class="required">
            <textarea name="l2_p3_comment" id="l2_p3_comment" cols="35" rows="5"></textarea>
          </div></td>
        <td valign="top"><input type="radio" name="l2_allowed" value="1" id="l2_allowed_2" /></td>
      </tr>
      <tr>
        <td valign="top"><strong>Pathway 4:</strong> Refer to Specialist</td>
        <td colspan="2"><div class="required">
            <textarea name="l2_p4_comment" id="l2_p4_comment" cols="35" rows="5"></textarea>
          </div></td>
        <td valign="top"><input type="radio" name="l2_allowed" value="1" id="l2_allowed_3" /></td>
      </tr>
      <tr>
        <td valign="top">&nbsp;</td>
        <td colspan="2">&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
          <th colspan="6">
        <div align="center"><strong>Level 3</strong></div>      </th>
      <tr>
        <td valign="top"><strong>Pathway</strong></td>
        <td colspan="2"><strong>Blocking or Accepting Comment</strong></td>
        <td valign="top"><strong>Appropriate</strong></td>
      </tr>
      <tr>
        <td width="26%" valign="top"><strong>Pathway 1:</strong> No action - See again</td>
        <td colspan="2"><div class="required">
            <textarea name="l3_p1_comment" id="l3_p1_comment" cols="35" rows="5"></textarea>
          </div></td>
        <td width="18%" valign="top"><p>
            <input type="radio" name="l3_allowed" value="1" id="l3_allowed_0" />
        </td>
      </tr>
      <tr>
        <td valign="top"><strong>Pathway 2:</strong> See again</td>
        <td colspan="2"><div class="required">
            <textarea name="l3_p2_comment" id="l3_p2_comment" cols="35" rows="5"></textarea>
          </div></td>
        <td valign="top"><input type="radio" name="l3_allowed" value="1" id="l3_allowed_1" /></td>
      </tr>
      <tr>
        <td valign="top"><strong>Pathway 3:</strong> Refer to ED</td>
        <td colspan="2"><div class="required">
            <textarea name="l3_p3_comment" id="l3_p3_comment" cols="35" rows="5"></textarea>
          </div></td>
        <td valign="top"><input type="radio" name="l3_allowed" value="1" id="l3_allowed_2" /></td>
      </tr>
      <tr>
        <td valign="top"><strong>Pathway 4:</strong> Refer to Specialist</td>
        <td colspan="2"><div class="required">
            <textarea name="l3_p4_comment" id="l3_p4_comment" cols="35" rows="5"></textarea>
          </div></td>
        <td valign="top"><input type="radio" name="l3_allowed" value="1" id="l3_allowed_3" /></td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td colspan="2"><input type="submit" name="submit" id="submit" value="Create Pathways for All Levels" /></td>
        <td>&nbsp;</td>
      </tr>
    </table>
  </form>

提交时,有多个插入:

$l1_p1_comment = $_POST['l1_p1_comment'];
$l1_p2_comment = $_POST['l1_p2_comment'];
$l1_p3_comment = $_POST['l1_p3_comment'];
$l1_p4_comment = $_POST['l1_p4_comment'];

$l2_p1_comment = $_POST['l2_p1_comment'];
$l2_p2_comment = $_POST['l2_p2_comment'];
$l2_p3_comment = $_POST['l2_p3_comment'];
$l2_p4_comment = $_POST['l2_p4_comment'];

$l3_p1_comment = $_POST['l3_p1_comment'];
$l3_p2_comment = $_POST['l3_p2_comment'];
$l3_p3_comment = $_POST['l3_p3_comment'];
$l3_p4_comment = $_POST['l3_p4_comment'];

$l1_allowed = $_POST['l1_allowed'];
$l2_allowed = $_POST['l2_allowed'];
$l3_allowed = $_POST['l3_allowed'];

if(isset($_POST['submit'])){ 

$query = 
"INSERT INTO pathway 
(pathway_pk, case_fk, level, pathway_allowed, comment, created, created_by) 
VALUES 
('', '$case_fk', '1', '$l1_allowed', '$l1_p1_comment', NOW(), '$author_pk'), 
('', '$case_fk', '1', '$l1_allowed', '$l1_p2_comment', NOW(), '$author_pk'),
('', '$case_fk', '1', '$l1_allowed', '$l1_p3_comment', NOW(), '$author_pk'), 
('', '$case_fk', '1', '$l1_allowed', '$l1_p4_comment', NOW(), '$author_pk'),
('', '$case_fk', '2', '$l2_allowed', '$l2_p1_comment', NOW(), '$author_pk'), 
('', '$case_fk', '2', '$l2_allowed', '$l2_p2_comment', NOW(), '$author_pk'),
('', '$case_fk', '2', '$l2_allowed', '$l2_p3_comment', NOW(), '$author_pk'), 
('', '$case_fk', '2', '$l2_allowed', '$l2_p4_comment', NOW(), '$author_pk'),
('', '$case_fk', '3', '$l3_allowed', '$l3_p1_comment', NOW(), '$author_pk'), 
('', '$case_fk', '3', '$l3_allowed', '$l3_p2_comment', NOW(), '$author_pk'),
('', '$case_fk', '3', '$l3_allowed', '$l3_p3_comment', NOW(), '$author_pk'), 
('', '$case_fk', '3', '$l3_allowed', '$l3_p4_comment', NOW(), '$author_pk')
";
$result = mysql_query($query, $connection) or die(mysql_error());

现在,我想让三个部分(第 1 级、第 2 级和第 3 级)中的每一个的无线电组为选定的无线电提交值“1”(即允许),每个组中的其他无线电提交“0”从:

$l1_allowed = $_POST['l1_allowed'];
$l2_allowed = $_POST['l2_allowed'];
$l3_allowed = $_POST['l3_allowed'];

关于如何做到这一点的任何建议?

4

2 回答 2

0

好的,我认为多次插入太难了,所以将三个区域分成单独的形式并使用:

$l1_allowed = intval($_POST['l1_allowed']);
$l2_allowed = intval($_POST['l2_allowed']);
$l3_allowed = intval($_POST['l3_allowed']);


if(isset($_POST['pathways_level_1'])){ 

$pathway_comment_l1 = array();

foreach($_POST['comment_l1'] as $comment_l1) {
    $pathway_comment_l1[]= mysql_real_escape_string($comment_l1);
}

for($i=0, $count = count($pathway_comment_l1);$i<$count;$i++) {
    $comment_l1 = $pathway_comment_l1[$i];
    $query_level_1 = sprintf(
        "INSERT INTO pathway (             
           pathway_pk,
           case_fk,
           level,
           pathway_allowed,
           comment
        ) VALUES (
           '',
           '$case_pk',
           '1',
           '%s',
           '$comment_l1')", $l1_allowed === $i ? 'y' : 'n');

       $result_level_1 = mysql_query($query_level_1, $connection) or die(mysql_error());
}
if($result_level_1){
$message = "- The pathways for level 1 have been created.";
}
}
?>
于 2013-02-28T06:39:29.793 回答
0

首先,正确转义您的输入

然后将处理表单数据的代码放到一个单独的页面中,例如 submit_action.php。并尽量避免使用if(isset($_POST['submit'])){. 反而:

if(isset($_POST['input1'])) {
$input1 = ($_POST['input1']);   
} else {
$input1 = "None";
}
if(isset($_POST['input2'])) {
$input1 = ($_POST['input2']);   
} else {
$input1 = "None";
}

<form action="submit_action.php" method="post" name="aFormName" id="form">

然后,在您的 INSERT 中:

$query="INSERT INTO $tbl_name(col1, col2)VALUES('$input1', '$input2')";

你指的是什么,无论如何?我在您的问题中没有看到页面链接。什么是验证()?

于 2013-02-28T03:00:56.690 回答