Shibbir
Shibbir

Reputation: 257

Php Showing Error Message If All Checkbox Is Not Checked (Multiple Checkbox)

In my Html form there are few Checkbox fields which user can select or not. Now if user select all Checkbox then the Form is showing success message BUT if all checkbox is not selected then it's showing several error message. So that it's not showing success message. Error message is look Like this :

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.....

My html Checkbox Form:

<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 form how I handle Checkbox :

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 I'm using to check all Checkbox within one row

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

Upvotes: 0

Views: 1648

Answers (2)

Indrajeet Singh
Indrajeet Singh

Reputation: 2989

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

Upvotes: 0

Bram Verstraten
Bram Verstraten

Reputation: 1557

Unchecked checkboxes are not posted. That's why you check this with isset() and then create the variable for the checkbox. But you don't create an empty variable if the checkbox is not checked. This is why you get a variable undefined notice.

Try changing your php code to something like

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

Upvotes: 2

Related Questions