2014-07-14 96 views
0

在我的Html表單中,有幾個複選框字段用戶可以選擇或不選擇。現在,如果用戶選擇所有複選框,然後窗體顯示成功消息,但如果沒有選中所有複選框,則顯示幾條錯誤消息。所以它沒有顯示成功消息。錯誤消息是這個樣子的:如果所有複選框未被選中,則顯示錯誤消息(多個複選框)

Notice: Undefined variable: mon_morning in D:\software installed\xampp installed\htdocs\tutor 
\tutor_request.php on line 142 

Notice: Undefined variable: mon_afternoon in D:\software installed\xampp installed\htdocs\tutor 
\tutor_request.php on line 142 

more..... 

我的HTML複選框形式:

<table width="500" border="0" cellspacing="0" cellpadding="0"> 
    <tr> 
    <td><input type="checkbox" name="mon_morning" value="mon_morning" id="checkAll"/>&nbsp;<strong>MON</strong></td> 
    <td><input type="checkbox" name="mon_morning" value="mon_morning" class="checkItem"/>&nbsp; Morning</td> 
    <td><input type="checkbox" name="mon_afternoon" value="mon_morning" class="checkItem"/>&nbsp; Afternoon</td> 
    <td><input type="checkbox" name="mon_evening" value="mon_evening" class="checkItem"/>&nbsp; Evening</td> 
    </tr> 
    <tr> 
    <td><input type="checkbox" name="tue_morning" value="tue_morning" id="checkAll_1"/>&nbsp;<strong>TUE</strong></td> 
    <td><input type="checkbox" name="tue_morning" value="tue_morning" class="checkItem_1"/>&nbsp; Morning</td> 
    <td><input type="checkbox" name="tue_afternoon" value="tue_morning" class="checkItem_1"/>&nbsp; Afternoon</td> 
    <td><input type="checkbox" name="tue_evening" value="tue_evening" class="checkItem_1"/>&nbsp; Evening</td> 
    </tr> 
    <tr> 
    <td><input type="checkbox" name="wed_morning" value="wed_morning" id="checkAll_2"/>&nbsp;<strong>WED</strong></td> 
    <td><input type="checkbox" name="wed_morning" value="wed_morning"/>&nbsp; Morning</td> 
    <td><input type="checkbox" name="wed_afternoon" value="wed_morning"/>&nbsp; Afternoon</td> 
    <td><input type="checkbox" name="wed_evening" value="wed_evening"/>&nbsp; Evening</td> 
    </tr> 
    <tr> 
    <td><input type="checkbox" name="thu_morning" value="thu_morning" id="checkAll_3"/> <strong>THU</strong></td> 
    <td><input type="checkbox" name="thu_morning" value="thu_morning"/>&nbsp; Morning</td> 
    <td><input type="checkbox" name="thu_afternoon" value="thu_morning"/>&nbsp; Afternoon</td> 
    <td><input type="checkbox" name="thu_evening" value="thu_evening"/>&nbsp; Evening</td> 
    </tr> 
    <tr> 
    <td><input type="checkbox" name="fri_morning" value="fri_morning" id="checkAll_4"/> <strong>FRI</strong></td> 
    <td><input type="checkbox" name="fri_morning" value="fri_morning"/>&nbsp; Morning</td> 
    <td><input type="checkbox" name="fri_afternoon" value="fri_morning"/>&nbsp; Afternoon</td> 
    <td><input type="checkbox" name="fri_evening" value="fri_evening"/>&nbsp; Evening</td> 
    </tr> 
    <tr> 
    <td><input type="checkbox" name="sat_morning" value="sat_morning" id="checkAll_5"/> <strong>SAT</strong></td> 
    <td><input type="checkbox" name="sat_morning" value="sat_morning"/>&nbsp; Morning</td> 
    <td><input type="checkbox" name="sat_afternoon" value="sat_morning"/>&nbsp; Afternoon</td> 
    <td><input type="checkbox" name="sat_evening" value="sat_evening"/>&nbsp; Evening</td> 
    </tr> 
    <tr> 
    <td><input type="checkbox" name="sun_morning" value="sun_morning" id="checkAll_6"/> <strong>SUN</strong></td> 
    <td><input type="checkbox" name="sun_morning" value="sun_morning"/>&nbsp; Morning</td> 
    <td><input type="checkbox" name="sun_afternoon" value="sun_morning"/>&nbsp; Afternoon</td> 
    <td><input type="checkbox" name="sun_evening" value="sun_evening"/>&nbsp; Evening</td> 
    </tr> 
</table> 

PHP表單我如何處理複選框:

if(isset($_POST['mon_morning'])) { $mon_morning = inputvalid($_POST['mon_morning']);} 
if(isset($_POST['mon_afternoon'])) $mon_afternoon = inputvalid($_POST['mon_afternoon']); 
if(isset($_POST['mon_evening'])) $mon_evening = inputvalid($_POST['mon_evening']); 
if(isset($_POST['tue_morning'])) $tue_morning = inputvalid($_POST['tue_morning']); 
// more..... 

的Javascript我使用檢查一行內的所有複選框

<script> 
$('td:first-child input[type="checkbox"]').on('change', function(){ 
    $(this) 
    .closest('td') 
    .siblings() 
    .find('input[type="checkbox"]').prop('checked', this.checked); 
}); 
</script> 
+0

什麼是你形成方法GET/POST? –

回答

2

未發佈複選框。這就是爲什麼你用isset()檢查這個,然後爲複選框創建變量。但是,如果未選中複選框,則不會創建空變量。這就是爲什麼你會得到一個未定義的變量。

試着改變你的PHP代碼類似

if(isset($_POST['mon_morning'])) { 
    $mon_morning = inputvalid($_POST['mon_morning']); 
} else { 
    $mon_morning = false; 
} 
+0

沒錯。它工作正常。 – Shibbir

+0

'isset'在這裏不是一個好選擇,因爲如果複選框被提交但它沒有設置值,它也會返回false。 'array_key_exists'是一個更好,更可靠的選擇。 – Synchro

0
Try this code: 
if (isset($_POST['mon_morning']) && !empty($_POST['mon_morning'])) { 
    $mon_morning = inputvalid($_POST['mon_morning']); 
} else { 
    $mon_morning = null; 
} 
相關問題