czukoman20 Posted June 30, 2009 Share Posted June 30, 2009 Hi all, I am working on a project that uses checkboxes to select all or some of the 50 states. I was wondering if there was an easy route to making them a variable that would be like. F_FP_$state and then that would show all the states they selected and nix all the states they didn't select. Examples of what i have already. <?php <input type="checkbox" name="AL[0]" id="list" value="AL" /> // That repeated 50 times with different state values. // When the user submits the checkboxes $AL =$_POST['AL']; foreach ($AL as $st_1){} // That repeated as well for each state. ?> Link to comment https://forums.phpfreaks.com/topic/164242-solved-where-to-go/ Share on other sites More sharing options...
rhodesa Posted June 30, 2009 Share Posted June 30, 2009 <?php <input type="checkbox" name="states[]" value="AL" /> // That repeated 50 times with different state values. // When the user submits the checkboxes $states = $_POST['states']; foreach ($states as $state){ echo $state; } ?> Link to comment https://forums.phpfreaks.com/topic/164242-solved-where-to-go/#findComment-866390 Share on other sites More sharing options...
czukoman20 Posted June 30, 2009 Author Share Posted June 30, 2009 Thanks, But for some reason when i did that.. it only echo's the last state that I selected. <?php <input type="checkbox" name="states[0]" id="list" value="AL" /> <input type="checkbox" name="states[0]" id="list" value="HI" /> <input type="checkbox" name="states0]" id="list" value="MA" /> <input type="checkbox" name="states[0]" id="list" value="NM" /> // output $states =$_POST['states']; foreach ($states as $state){ echo "$state"; } // it echos NM when those 4 are selected? ?> also the twist to this is.. there are categories.. so each state needs its own Category Information F_ = Financial FP_ = Subcategory of Financial So those need to be F_FP_$state But it needs to repeat itself like that for however many states are selected.. If they didn't select a state I dont want the database to have a bunch of blank "F_FP_ " Thanks for the help so far Link to comment https://forums.phpfreaks.com/topic/164242-solved-where-to-go/#findComment-866397 Share on other sites More sharing options...
rhodesa Posted June 30, 2009 Share Posted June 30, 2009 don't put a 0 in the square brackets. but using a 0, you are telling them to all use the same index of 0, with empty backets it will anonymously assign indexes let's start with that before getting into the categories...if you get rid of the 0 does it work as you would expect? Link to comment https://forums.phpfreaks.com/topic/164242-solved-where-to-go/#findComment-866403 Share on other sites More sharing options...
czukoman20 Posted June 30, 2009 Author Share Posted June 30, 2009 Yes it does. Now the output shows ALHINAMH thanks Cool Now for the categories... It wouldn't be as hard, but the page changes according to what the input in the url is.. Here is what i have so far <?php if ($cat1 == "FP_"){ $input = "F_FP_$state"; } // of course that doesn't work lol ?> I'm thinking array, but idunno how to do that. Link to comment https://forums.phpfreaks.com/topic/164242-solved-where-to-go/#findComment-866418 Share on other sites More sharing options...
rhodesa Posted June 30, 2009 Share Posted June 30, 2009 like this? <?php $states =$_POST['states']; if ($cat1 == "FP_"){ $input = array(); foreach($states as $state){ $input[] = "F_FP_$state"; } } ?> Link to comment https://forums.phpfreaks.com/topic/164242-solved-where-to-go/#findComment-866437 Share on other sites More sharing options...
czukoman20 Posted June 30, 2009 Author Share Posted June 30, 2009 SWEEET!! Thanks a ton.. Just one final question. The only way i know how to "echo" this is throught another foreach statement. Also there is print_r(); Would I have to say print_r($input[1,2,3,4,5,etc..]); or is there a more simplified way? Thanks for your help!! Link to comment https://forums.phpfreaks.com/topic/164242-solved-where-to-go/#findComment-866460 Share on other sites More sharing options...
rhodesa Posted June 30, 2009 Share Posted June 30, 2009 how do you want to echo it? comma separated? echo implode(',',$input); Link to comment https://forums.phpfreaks.com/topic/164242-solved-where-to-go/#findComment-866465 Share on other sites More sharing options...
czukoman20 Posted June 30, 2009 Author Share Posted June 30, 2009 interesting the output is now F_FP_AL F_FP_AL ,F_FP_HI F_FP_AL ,F_FP_HI ,F_FP_MA F_FP_AL ,F_FP_HI ,F_FP_MA ,F_FP_NM Wierd, I bolded the States to make it easier to read The code I am using is <?php <input type="checkbox" name="states[]" id="list" value="AL" /> <input type="checkbox" name="states[]" id="list" value="HI" /> <input type="checkbox" name="states[]" id="list" value="MA" /> <input type="checkbox" name="states[]" id="list" value="NM" /> //output $states =$_POST['states']; if ($cat1 == "FP_"){ $input = array(); foreach($states as $state){ $input[] = "F_FP_$state "; echo implode(',',$input); } } ?> Link to comment https://forums.phpfreaks.com/topic/164242-solved-where-to-go/#findComment-866478 Share on other sites More sharing options...
czukoman20 Posted June 30, 2009 Author Share Posted June 30, 2009 Nevermind I figured it out... I had to put it on the outside of the Brackets... Ok Thanks a ton for your help!!! Nice site btw Have a good one! Link to comment https://forums.phpfreaks.com/topic/164242-solved-where-to-go/#findComment-866486 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.