femiot Posted March 27, 2012 Share Posted March 27, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/259802-validating-and-retaining-state-of-a-dynamic-checkboxes/ Share on other sites More sharing options...
seanlim Posted March 27, 2012 Share Posted March 27, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/259802-validating-and-retaining-state-of-a-dynamic-checkboxes/#findComment-1331521 Share on other sites More sharing options...
cpd Posted March 27, 2012 Share Posted March 27, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/259802-validating-and-retaining-state-of-a-dynamic-checkboxes/#findComment-1331523 Share on other sites More sharing options...
femiot Posted March 27, 2012 Author Share Posted March 27, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/259802-validating-and-retaining-state-of-a-dynamic-checkboxes/#findComment-1331623 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.