Tom Tom - 5 months ago 13
PHP Question

Form action based on Input Type selection

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.

F0G F0G
Answer

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
}
Comments