我有具有由如下的函數生成20下拉字段一個html形式....驗證在PHP爲下拉菜單
<?php
$sub_1 = subject_show (subject_1, $scl_unique_id, $cls_sec_bat);
if ($sub_1)
{?>
<tr>
<td><?php echo $sub_1;?></td>
<td><select name="subject_1" id="subject_1" style="min-width:100px">
<?php
if(isset($subject_1)){?>
<option value="<?php echo $subject_1; ?>" selected="selected"><?php echo $subject_1; ?></option>
<?php }else{ ?>
<option value="<?php echo $sub_1;?>" disabled="disabled" selected="selected"><?php echo $sub_1;?></option>
<?php } ?>
<option value="<?php echo MAR_ABS;?>"><?php echo MAR_ABS;?></option>
<option value="<?php echo MAR_NA;?>"><?php echo MAR_NA;?></option>
<?php
for ($i=0; $i<=100; $i++)
{
echo '<option value="'.$i.'">'.$i.'</option>';
}
?>
</select></td>
</tr>
<?php
}
?>
在代碼中,如果函數對$ sub_1的值,則打印在帶有名稱subject_1的html輸出上的下拉字段。如果不是,它將不會打印。
相同的代碼我有20次。如果函數的值爲$ sub_2或$ sub_3或$ sub_4,直到$ sub_20,則該字段的名稱爲subject_2或subject_3或subject_4,直到subject_20。
根據用戶輸入和功能輸出,可以打印任何下拉字段。
當閱讀時間,我讀所有的20個名字,如
if (isset($_POST['save_exit']))
{
$roll_number = strtoupper(trim(implode(' ', preg_split('/\s+/', $_POST["roll_number"]))));
$subject_1 = $_POST["subject_1"];
$subject_2 = $_POST["subject_2"];
$subject_3 = $_POST["subject_3"];
$subject_4 = $_POST["subject_4"];
$subject_5 = $_POST["subject_5"];
$subject_6 = $_POST["subject_6"];
$subject_7 = $_POST["subject_7"];
$subject_8 = $_POST["subject_8"];
$subject_9 = $_POST["subject_9"];
$subject_10 = $_POST["subject_10"];
$subject_11 = $_POST["subject_11"];
$subject_12 = $_POST["subject_12"];
$subject_13 = $_POST["subject_13"];
$subject_14 = $_POST["subject_14"];
$subject_15 = $_POST["subject_15"];
$subject_16 = $_POST["subject_16"];
$subject_17 = $_POST["subject_17"];
$subject_18 = $_POST["subject_18"];
$subject_19 = $_POST["subject_19"];
$subject_20 = $_POST["subject_20"];
我不希望用戶提交空的下拉列表,所以我wrinting審定
if(empty(subject_1))
{
say error
}
if(empty(subject_2))
{
say error
}
我wrting這所有科目20次。
問題出在用戶在論壇中只有三個主題,如subject_1和subject_2和subject_3。它會給所有剩餘的17個主題帶來錯誤。
我該如何閱讀只驗證表單上可用的主題?
請讓我知道,如果它不明確。
請張貼什麼樣的HTML表單看起來像PHP使得它後(從頁面源)爲例。這是在表單元素'name ='屬性中使用PHP的'[]'數組語法重新構建的。 –
我已經發布了表單代碼。請讓我知道你想要什麼?我怎麼才能得到它.. – user2642907
您需要檢查表單元素是否已發佈,但值爲空 - 'isset($ _ POST [「subject _#」])''''subject_1 =='''。所以你的'if'可以是 - 'if(isset($ _ POST [「subject_1」] && empty($ subject_1)){error message}' – Sean