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
<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.
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
}
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
}
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
<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';
}
?>
<?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
}
}