2013-02-28 57 views
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']; 

關於如何做到這一點的任何建議?

回答

0

首先,escape your inputs correctly

然後把你的代碼處理表單的數據到一個單獨的頁面,比如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')"; 

而你指向的onsubmit什麼,反正?我在你的問題中看不到頁面鏈接。什麼是驗證()?

+0

謝謝,但是這並沒有回答我的問題。順便說一句,表單提交到當前頁面......沒有問題做插入,只需要建議如何處理多個插入的無線電溝通... validate()是一個js scipt來驗證窗體...不包括在上面的代碼中不相關。 – IlludiumPu36 2013-02-28 03:19:49

0

OK,我決定這是與多個插入太難了,所以分割成3個區成單獨的形式和使用:

$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."; 
} 
} 
?>