Number7 Posted February 9, 2012 Share Posted February 9, 2012 Hi, I would like to know if there's a way to quickly collect and sort form data, then use that data in an if statement. I've tried for a while now and i just can't seem to figure it out. I would like to do this with radio buttons. Hopefully this will make it clear: Code: <?php if (isset($_POST['submit'])) { // I don't know how to do this but maybe something like // if $_POST values == "Yes" // put them into an array // then count how many are in the array, e.g. 2 if 2 "Yes" radio buttons are selected $count = count($array) . " Yes values selected"; echo $count; //else if do the same for "No" values // hopefully this is understandable } ?> <!-- i want to take these values and group them, so all the "Yes" values selected together and the "no" values together --> <form method="post" action=""> <input type="radio" name="first" value="Yes" /> <input type="radio" name="first" value="No" /> <input type="radio" name="second" value="Yes" /> <input type="radio" name="second" value="No" /> <input type="submit" name="submit"> </form> Quote Link to comment https://forums.phpfreaks.com/topic/256772-grouping-and-adding-form-values/ Share on other sites More sharing options...
freelance84 Posted February 9, 2012 Share Posted February 9, 2012 //determine the poss radio buttons $arrayOfPossibleRadios = array('first','second','third','fourth'); $yesCounter = 0; foreach($_POST as $key => $value) { if(in_array($key,$arrayOfPossibleRadios)) { if($value == 'yes') { ++$yesCounter; } } } echo $yesCounter; Quote Link to comment https://forums.phpfreaks.com/topic/256772-grouping-and-adding-form-values/#findComment-1316351 Share on other sites More sharing options...
PaulRyan Posted February 10, 2012 Share Posted February 10, 2012 Here is an example that I threw together: <?PHP //### Set-up the array of check boxes $checkBoxes = array('first','second','third','forth'); //### Check if the page was requested by post, if so process incoming data if($_SERVER['REQUEST_METHOD'] == 'POST') { //### Initialize the array for "yes" and "no" $yesArray = array(); $noArray = array(); //### If $_POST['radio'] is set, use it, if not, initialize empty array $radiosChecked = isSet($_POST['radio']) ? $_POST['radio'] : array() ; //### Itterate through each radio button and assign it to the correct array foreach($radiosChecked AS $name => $value) { if($value) { $yesArray[] = $name; } else { $noArray[] = $name; } } //### Echo out the 2 arrays echo '<pre>'; echo '<u>Selected Yes</u><br>'; print_r($yesArray); echo '<br><u>Selected No</u><br>'; print_r($noArray); echo '</pre>'; } ?> <form method="POST" action=""> <?PHP foreach($checkBoxes AS $name) { echo $name.'<br>'; echo ' <input type="radio" name="radio['.$name.']" value="1"> Yes <br>'; echo ' <input type="radio" name="radio['.$name.']" value="0"> No <br><br>'; } ?> <input type="submit" value="Submit Form"> </form> Hopefully this will guide you along the way to finding a correct solution to your problem Regards, PaulRyan. Quote Link to comment https://forums.phpfreaks.com/topic/256772-grouping-and-adding-form-values/#findComment-1316370 Share on other sites More sharing options...
Number7 Posted February 10, 2012 Author Share Posted February 10, 2012 Thanks a lot for the replies it reeeally helped. I ended up doing something like this in case anyone wants to know (added a bit in case it helps others): <?php if (isset($_POST['submit'])) { $radios = array ($_POST['first'], $_POST['second'], $_POST['third']); $yesCounter = 0; $noCounter = 0; $maybeCounter = 0; foreach($radios as $key => $value) { if ($value == "Yes"){ ++$yesCounter; } else if ($value == "No"){ ++$noCounter; } else if ($value == "Maybe"){ ++$maybeCounter; } }echo $yesCounter, " Yes <br>", $noCounter, " No <br>", $maybeCounter, " Maybe <br>"; } ?> <form method="post" action=""> <input type="radio" name="first" value="Yes" /><br> <input type="radio" name="first" value="No" /> <br> <input type="radio" name="first" value="Maybe" /> <br><br> <input type="radio" name="second" value="Yes" /> <br> <input type="radio" name="second" value="No" /> <br> <input type="radio" name="second" value="Maybe" /> <br><br> <input type="radio" name="third" value="Yes" /> <br> <input type="radio" name="third" value="No" /> <br> <input type="radio" name="third" value="Maybe" /> <br><br> <input type="submit" name="submit"> </form> Quote Link to comment https://forums.phpfreaks.com/topic/256772-grouping-and-adding-form-values/#findComment-1316469 Share on other sites More sharing options...
PaulRyan Posted February 10, 2012 Share Posted February 10, 2012 Glad you got something worked out, just a little note though... Try not to rely on the submit button name and value being sent, as not all browsers send that information along with the form. In my example I used this: if($_SERVER['REQUEST_METHOD'] == 'POST') { You should try using that, or if there are multiple forms, use a hidden field like this: <input type="hidden" name="action" value="ACTION VALUE HERE"> Regards, PaulRyan. Quote Link to comment https://forums.phpfreaks.com/topic/256772-grouping-and-adding-form-values/#findComment-1316550 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.