Form action based on Input Type selection

2019-08-08 10:52发布

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.

标签: php html forms
4条回答
贪生不怕死
2楼-- · 2019-08-08 10:59

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
}
查看更多
狗以群分
3楼-- · 2019-08-08 11:02

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
}
查看更多
地球回转人心会变
4楼-- · 2019-08-08 11:03

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';
}
?>
查看更多
Juvenile、少年°
5楼-- · 2019-08-08 11:17
<?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
    }
}
查看更多
登录 后发表回答