1

我正在“index.php”页面上的 php 中创建一个表单。加载页面时,将创建以下表单:

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

当用户点击提交按钮时,会显示相应的内容:

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

回到表格,注意单选选项:

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

我是否可以在 if 语句中放置一个条件,以根据从单选按钮中选择的是非聚合还是聚合来执行不同的操作;如果是这样,我将如何去做?

非常感谢您的帮助。

4

4 回答 4

4

首先,为您的收音机指定一个“名称”。两者都一样。

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

在“else”子句上,做一个小改动,插入

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

利用

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

最后但并非最不重要的一点是,如果...

if ($_POST['aggregation'] == 'Aggregated')
{
// Actions for Aggregated
}
else
{
// Actions for Non-Aggregated
}
于 2013-07-02T10:41:53.930 回答
4

为单选按钮赋予名称属性,例如

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

执行 POST 方法后,您可以使用 PHP 检查值:

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

以其他方式,您可以设置名称,例如

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

并检查这个如果isset

if(isset($_POST['Aggregated'])){
    //DO STUFF
}
if(isset($_POST['Non-Aggregated'])){
  //DO OTHER STUFF
}
于 2013-07-02T10:44:03.157 回答
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
    }
}
于 2013-07-02T10:43:18.257 回答
3

这是工作代码:

<?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';
}
?>
于 2013-07-02T10:44:45.907 回答