0
votes

Here's my problem: I got 11 checkbox on my website for a form creating accounts. When I submit the form (which is POST on the same page) I verify that mandatory input are there. In the case one is missing (ex. the password), I want to keep information that has been entered before INCLUDING checkbox. My problem is: How?

Here's my checkbox:

<table class="adroite">
      <tr><td>NES<input type="checkbox" name="chkcons[]" value="NES"/></td>
      <td>SNES<input type="checkbox" name="chkcons[]" value="SNES"/></td>
                <td>N64<input type="checkbox" name="chkcons[]" value="N64"/></td>
            </tr>
            <tr>
                <td>NGC<input type="checkbox" name="chkcons[]" value="NGC"/></td>
                <td>Wii  <input type="checkbox" name="chkcons[]" value="Wii"/></td>
                <td>WiiU  <input type="checkbox" name="chkcons[]" value="WiiU"/> </td>
            </tr>
            <tr>
                <td>GB <input type="checkbox" name="chkcons[]" value="GB"/></td>
                <td>GBC <input type="checkbox" name="chkcons[]" value="GBC"/></td>
                <td>GBA  <input type="checkbox" name="chkcons[]" value="GBA"/></td>
            </tr>
         <tr>
             <td>DS <input type="checkbox" name="chkcons[]" value="DS"/></td>
             <td>3DS <input type="checkbox" name="chkcons[]" value="3DS"/></td>
         </tr>
        </table>

You see that checkbox are name chkcons[]. I use this method to save the results in MYSQL after the checkup.

So, how can I tell my PHP: See, this checkbox was checked, so keep it check after submit? Thanks!

2
put some logic into your PHP to see what was submitted and add a selected="selected" attribute to the checkboxes as appropriate.Marc B

2 Answers

1
votes

Quick example

<input type="checkbox" name="chkcons[]" value="NES" <?php if(in_array('NES', $_POST['chkcons'])){ ?>selected="selected"<?php } ?>/>

0
votes

Why not to validate before submission using some jQuery or JS?

Check this

for checkBox validation check here

and if you want to do that using PHP(not a good move) then this Thread will be helpful.

but I prefer to do this using JS