1

I am creating a form in php on the page "index.php". When the page is loaded the following form is created:

if($_SERVER['REQUEST_METHOD']!='POST')
{
        echo '
        <form action="index.php" method="POST">
        <fieldset style="width: 700px;">
        <legend>Enter your search below</legend>
        <textarea rows="1" cols="80" name="query">
        </textarea>
        </fieldset>
        <p>
        <input type="radio" value="Non-Aggregated"> Non-Aggregated
        <input type="radio" value="Aggregated"> Aggregated 
        &nbsp;
        &nbsp;
        <input type="submit" value="Search">
        </p>
        </form>';   
}

When the user clicks the submit button, the appropriate content is displayed:

else
{
    if ($_POST['query'])
    {
        //content displayed after form submission
    }
}

Going back to the form, note the radio options:

<input type="radio" value="Non-Aggregated"> Non-Aggregated
<input type="radio" value="Aggregated"> Aggregated

Is there a condition that I can place in the if-statement to carry out a different action based on whether Non-Aggregated or Aggregated is selected from the radio buttons; and if so how would I go about doing this?

Thanks for any help at all.

MrCode
  • 63,975
  • 10
  • 90
  • 112
Tom
  • 561
  • 8
  • 16
  • 1
    you can change it with javascript but what do you want to do at all? – Andreas Linden Jul 02 '13 at 10:35
  • @AndreasLinden if the user selects Non-Aggregated I will display certain information, if they select Aggregated I will display other information. – Tom Jul 02 '13 at 10:38
  • 1
    well so do a switch in on the post-field in php, don't change the html... see my upcomming answer – Andreas Linden Jul 02 '13 at 10:40
  • Hi, sorry I made a small but vital edit, the else statement should have read `if ($_POST['query'])` not `if($_SERVER['REQUEST_METHOD']!='POST')` – Tom Jul 02 '13 at 11:05

4 Answers4

4

First of all, asign a "name" to your radios. Same to both.

<input type="radio" name="aggregation" value="Non-Aggregated"> Non-Aggregated
<input type="radio" name="aggregation" value="Aggregated"> Aggregated 

On the "else" clause, make a small change, insted of

if($_SERVER['REQUEST_METHOD']!='POST')

use

if($_SERVER['REQUEST_METHOD']=='POST')

And last, but not least, add your condition inside that last if...

if ($_POST['aggregation'] == 'Aggregated')
{
// Actions for Aggregated
}
else
{
// Actions for Non-Aggregated
}
ivanargulo
  • 326
  • 1
  • 5
4

Give name attribute to radio buttons, like

<input type="radio" name="aggr" value="Non-Aggregated"> Non-Aggregated
<input type="radio" name="aggr" value="Aggregated"> Aggregated

After POST method is executed you can check values with PHP:

if($_POST['aggr']=='Aggregated'){
//DO STUFF
}
if($_POST['aggr']=='Non-Aggregated'){
//DO OTHER STUFF
}

In other way, you can set names, like

<input type="radio" name="Non-Aggregated" value="Non-Aggregated"> Non-Aggregated
<input type="radio" name="Aggregated" value="Aggregated"> Aggregated

And check this if isset

if(isset($_POST['Aggregated'])){
    //DO STUFF
}
if(isset($_POST['Non-Aggregated'])){
  //DO OTHER STUFF
}
loler
  • 2,594
  • 1
  • 20
  • 30
  • 1
    Thanks @FOG, I have tested your first way and it works. Other answers seem to do similar stuff, so thank you everybody :) – Tom Jul 02 '13 at 11:25
3
<?php
if($_SERVER['REQUEST_METHOD']!='POST')
{
    // echo the form and don't forget to give the radio inputs a name
    // <input name="agg" type="radio" value="Non-Aggregated"> Non-Aggregated
    // <input name="agg" type="radio" value="Aggregated"> Aggregated 
}
else if (isset($_POST['agg']))
{
    if ($_POST['agg'] == 'Aggregated') {
        // do one thing
    } else {
        // do something else
    }
}
Andreas Linden
  • 12,489
  • 7
  • 51
  • 67
  • 3
    I would modify the conditional to `if(isset($_POST['agg']) && $_POST['aggr'] == 'Aggregated')` for a piece of mind – n1te Jul 02 '13 at 10:55
  • 1
    just added it to the outer condition as if it's not set at all you don't want to perform the action for "not-aggregated" – Andreas Linden Jul 02 '13 at 13:16
3

Here is working code:

<?php if($_SERVER['REQUEST_METHOD']!='POST')
{
    $form = <<<FORM
<form action="" method="POST">
<fieldset style="width: 700px;">
<legend>Enter your search below</legend>
<textarea rows="1" cols="80" name="query">
</textarea>
</fieldset>
<p>
<input type="radio" value="Non-Aggregated" name='radioCheck'> Non-Aggregated
<input type="radio" value="Aggregated" name='radioCheck'> Aggregated 
&nbsp;
&nbsp;
<input type="submit" value="Search">
</p>
</form>
FORM;
echo $form;
}
elseif($_SERVER['REQUEST_METHOD']=='POST' && isset($_POST['radioCheck']) && $_POST['radioCheck'] =='Non-Aggregated'){

// Non-Aggregated form
    echo 'Non-Aggregated';

}elseif($_SERVER['REQUEST_METHOD']=='POST' && isset($_POST['radioCheck']) && $_POST['radioCheck'] =='Aggregated'){
// Aggregated form

    echo 'Aggregated';
}
?>
Nono
  • 6,986
  • 4
  • 39
  • 39