simplyrichard Posted October 1, 2008 Share Posted October 1, 2008 Can someone help me format these two codes together? <?PHP $query="SELECT activities_id, activities_name FROM culvercareers.activities"; $result = mysql_query ($query); while($nt=mysql_fetch_array($result)) { //Array or records stored in echo "<input type=checkbox name=activities value=$nt[activities_id]> $nt[activities_name] <br>"; } echo "</select>"; ?> (The above code pulls the activities from the database.) and .... <?php if ($_POST['activities'] == $nt[activities_name]) { echo "checked"; } ?> What I am trying to do is if an error happens, the checkboxes on my form are still checked.... Link to comment https://forums.phpfreaks.com/topic/126631-keeping-checkboxes-checked-after-post/ Share on other sites More sharing options...
F1Fan Posted October 1, 2008 Share Posted October 1, 2008 What errors? Do you mean you want it to always be checked by default? Link to comment https://forums.phpfreaks.com/topic/126631-keeping-checkboxes-checked-after-post/#findComment-654829 Share on other sites More sharing options...
simplyrichard Posted October 1, 2008 Author Share Posted October 1, 2008 No, after you hit submit on my form, it brings you back to the form to correct any errors but all checkboxes, and dropdowns are uncheacked or emtpy. Link to comment https://forums.phpfreaks.com/topic/126631-keeping-checkboxes-checked-after-post/#findComment-654832 Share on other sites More sharing options...
F1Fan Posted October 1, 2008 Share Posted October 1, 2008 OK, I see. First of all, you need [] brackets after "activities" in your checkbox name, so it will be treated as an array. If you don't do that, you'll only get one value of all the checked values. Then, you'll need to check that array for matching values. This should work: <?php $activities = isset($_POST['activities'])?$_POST['activities']:array(); $query="SELECT activities_id, activities_name FROM culvercareers.activities"; $result = mysql_query ($query); while($nt=mysql_fetch_array($result)) { //Array or records stored in echo "<input type='checkbox' name='activities[]'".(in_array($nt['activities_id'],$activities)?" checked":"")." value='{$nt['activities_id']}'> {$nt['activities_name']} <br>"; } ?> I also fixed a lot of your syntax. Be careful about that, because using incorrect syntax may work, but then something may go wrong down the road that will screw you up. Link to comment https://forums.phpfreaks.com/topic/126631-keeping-checkboxes-checked-after-post/#findComment-654844 Share on other sites More sharing options...
aebstract Posted October 1, 2008 Share Posted October 1, 2008 Just check to see if $_POST['whatever'] is set, if it is then make your checkbox with 'checked' in it and if it isn't then make your normal checkbox. Link to comment https://forums.phpfreaks.com/topic/126631-keeping-checkboxes-checked-after-post/#findComment-654846 Share on other sites More sharing options...
simplyrichard Posted October 1, 2008 Author Share Posted October 1, 2008 THanks a million that code worked perfect! I didnt write that bit of code sorry for the errors. Richard Link to comment https://forums.phpfreaks.com/topic/126631-keeping-checkboxes-checked-after-post/#findComment-654859 Share on other sites More sharing options...
simplyrichard Posted October 2, 2008 Author Share Posted October 2, 2008 F1Fan , Using your code above, it worked perfect. However now when I try to insert the selected activities into the database I get only "array" inserted. I assume I need to do something like this $aoi = $activities['0'].$activities['1'].$activities['2'].$activities['3']; That didnt work nor did $aoi = $_POST['activities']; Can you help? Richard Link to comment https://forums.phpfreaks.com/topic/126631-keeping-checkboxes-checked-after-post/#findComment-655581 Share on other sites More sharing options...
F1Fan Posted October 2, 2008 Share Posted October 2, 2008 $_POST['activities'] is an array, so you can't just insert it into the DB like that. You'll have to use a foreach loop. Link to comment https://forums.phpfreaks.com/topic/126631-keeping-checkboxes-checked-after-post/#findComment-655658 Share on other sites More sharing options...
simplyrichard Posted October 2, 2008 Author Share Posted October 2, 2008 Can you give me an example of how to call out each item in the array? Link to comment https://forums.phpfreaks.com/topic/126631-keeping-checkboxes-checked-after-post/#findComment-655744 Share on other sites More sharing options...
F1Fan Posted October 2, 2008 Share Posted October 2, 2008 <?php $activities = $_POST['activities']; foreach ($activities as $activity){ // run your insert queries here... } ?> Link to comment https://forums.phpfreaks.com/topic/126631-keeping-checkboxes-checked-after-post/#findComment-655752 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.