Jump to content

Validating and retaining state of a dynamic checkboxes


femiot

Recommended Posts

Am new to php... I have been battling on my dynamic checkboxes in such a way that if none is checked the form is return, also I need to retain what was checked when the form postback due to other invalid inputs.

 

$result = mysql_query("SELECT * FROM course") or die(mysql_error()); if ($result) { while ($row = mysql_fetch_array($result)){ if (isset($_POST['courses']) and $_POST['courses'] == $row['cid']) {echo $row['cid'];} print "<input type=\"checkbox\" name=\"courses[]\" value=\"$row[cid]\">$row[cname]\n"; } }

 

Help needed purely on php codes. Thanks in advance

 

Link to comment
Share on other sites

It would help greatly if you indented your code nicely!

 

$result = mysql_query("SELECT * FROM course") or die(mysql_error());
if ($result)
    while ($row = mysql_fetch_array($result))
    {
        print "<input type=\"checkbox\" name=\"courses[]\" value=\"$row[cid]\"".(isset($_POST['courses']) and in_array($row['cid'],$_POST['courses'])) ? ' checked' : '') .">$row[cname]\n"; 
    }

 

*untested code

 

The basic idea here is that you check if the id of the checkbox exists in the $_POST['courses'] array. If it does, you assume that the box was checked in the form submission, and print the "checked" attribute within the checkbox's HTML tag.

 

Link to comment
Share on other sites

You haven't actually outlined your issue? If your trying to run a check to ensure at least one checkbox is ticked you could do it in various ways. I'd prefer an array in the follow manner.

 

<input type="checkbox" name="checkbox[course1]" />
<input type="checkbox" name="checkbox[course2]" />
<input type="checkbox" name="checkbox[course3]" />

 

And in my PHP

 

if(isset($_POST['checkbox'])){ // Do something

 

You can then test if only "checkbox". Cycle through the checkboxes to determine which ones are set.

 

Furthermore, if your form action redirects to another page you will need to set the post data to a session and test for it in your input fields.

 

Your new to PHP so that concept may be a little daunting. If you don't understand let me know but do still explain your issue further as its currently very vague and more help may be given from other members if you explain in far more detail.

Link to comment
Share on other sites

thanks guys... Have actually sorted it out.

 

$result = mysql_query("SELECT * FROM course") or die(mysql_error());  
if ($result){ 
    while ($row = mysql_fetch_array($result)){ 
         $checked = 'unchecked';
	 if ($_POST['courses'] != "")
	 {
        if (isset($_POST['courses'])){ 
           if (in_array($row['cid'], $_POST['courses'])){ 
               $checked = "checked"; 
           } 
        } 
	}
        echo "<input type=\"checkbox\" name=\"courses[]\"  value=\"$row[cid]\" $checked >$row[cname]\n";  
    } 
} 

thanks once again

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.