Hello All,

I am new to PHP and the requirement is, i need to design a form with multiple checkboxes with same name. I tried using arrays and the code is

<td>
 <input type="checkbox" name="eroom_status[]" value="empty" 
  <?php if(isset($_POST['eroom_status'])) {
   if (in_array('empty',$POST['eroom_status']) {
        $check = 'checked';}
   }  echo $check;?>  >
   </td>

                                    <td>
                                        <input type="checkbox" name="eroom_status[]" value="Rejected"
                                        <?php  if(isset($_POST['eroom_status'])) {
                                            if (in_array('Rejected',$POST['eroom_status'])) { $check = 'checked'; }
                                               }?> <?php echo $check;?> >
                                    </td>
                                    <td>
                                        <input type="checkbox" name="eroom_status[]" value="New To Airbus"
                                        <?php if(isset($_POST['eroom_status'])) {
                                            if (in_array('New To Airbus',$POST['eroom_status'])) { $check = 'checked'; }
                                        }  echo $check;?> >
                                    </td>
                                    <td>
                                        <input type="checkbox" name="eroom_status[]" value="Closed"
                                        <?php  if(isset($_POST['eroom_status'])) {
                                            if (in_array('Closed',$POST['eroom_status'])) { $check = 'checked'; }
                                        } echo $check; ?> >
                                    </td>
                                    <td> <input type="checkbox" name="eroom_status[]" value="Closed with Comments"
                                        <?php  if(isset($_POST['eroom_status'])) {
                                            if (in_array('Closed with Comments',$POST['eroom_status'])) { $check = 'checked'; }
                                        } echo $check; ?> >
                                    </td>.

I am getting an error message. In in_array() function the second parameter is null. Please help me regarding this issue.

Member Avatar for nileshgr

You're using $POST instead of $_POST. Use this corrected code-

<td>
 <input type="checkbox" name="eroom_status[]" value="empty" 
  <?php if(isset($_POST['eroom_status'])) {
   if (in_array('empty',$_POST['eroom_status']) {
        $check = 'checked';}
   }  echo $check;?>  >
   </td>

                                    <td>
                                        <input type="checkbox" name="eroom_status[]" value="Rejected"
                                        <?php  if(isset($_POST['eroom_status'])) {
                                            if (in_array('Rejected',$_POST['eroom_status'])) { $check = 'checked'; }
                                               }?> <?php echo $check;?> >
                                    </td>
                                    <td>
                                        <input type="checkbox" name="eroom_status[]" value="New To Airbus"
                                        <?php if(isset($_POST['eroom_status'])) {
                                            if (in_array('New To Airbus',$_POST['eroom_status'])) { $check = 'checked'; }
                                        }  echo $check;?> >
                                    </td>
                                    <td>
                                        <input type="checkbox" name="eroom_status[]" value="Closed"
                                        <?php  if(isset($_POST['eroom_status'])) {
                                            if (in_array('Closed',$_POST['eroom_status'])) { $check = 'checked'; }
                                        } echo $check; ?> >
                                    </td>
                                    <td> <input type="checkbox" name="eroom_status[]" value="Closed with Comments"
                                        <?php  if(isset($_POST['eroom_status'])) {
                                            if (in_array('Closed with Comments',$_POST['eroom_status'])) { $check = 'checked'; }
                                        } echo $check; ?> >
                                    </td>.

Hello itech7,

Thats a good finding..that too in a badly formated code... Thanku very much:)

You're using $POST instead of $_POST. Use this corrected code-

<td>
 <input type="checkbox" name="eroom_status[]" value="empty" 
  <?php if(isset($_POST['eroom_status'])) {
   if (in_array('empty',$_POST['eroom_status']) {
        $check = 'checked';}
   }  echo $check;?>  >
   </td>

                                    <td>
                                        <input type="checkbox" name="eroom_status[]" value="Rejected"
                                        <?php  if(isset($_POST['eroom_status'])) {
                                            if (in_array('Rejected',$_POST['eroom_status'])) { $check = 'checked'; }
                                               }?> <?php echo $check;?> >
                                    </td>
                                    <td>
                                        <input type="checkbox" name="eroom_status[]" value="New To Airbus"
                                        <?php if(isset($_POST['eroom_status'])) {
                                            if (in_array('New To Airbus',$_POST['eroom_status'])) { $check = 'checked'; }
                                        }  echo $check;?> >
                                    </td>
                                    <td>
                                        <input type="checkbox" name="eroom_status[]" value="Closed"
                                        <?php  if(isset($_POST['eroom_status'])) {
                                            if (in_array('Closed',$_POST['eroom_status'])) { $check = 'checked'; }
                                        } echo $check; ?> >
                                    </td>
                                    <td> <input type="checkbox" name="eroom_status[]" value="Closed with Comments"
                                        <?php  if(isset($_POST['eroom_status'])) {
                                            if (in_array('Closed with Comments',$_POST['eroom_status'])) { $check = 'checked'; }
                                        } echo $check; ?> >
                                    </td>.
Member Avatar for nileshgr

I manually didn't replace each error. Since I use Linux, I have all sorts of text processing utitilies available on the command line. I replaced all of them with just one line. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.