Jump to content

grouping and adding form values


Number7

Recommended Posts

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>

 

Link to comment
Share on other sites

//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;

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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.

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.