我正在页面上的php中创建一个表单" index.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
<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语句中执行不同的操作?从单选按钮中选择 ;如果是这样,我该怎么做呢?
感谢您提供任何帮助。
答案 0 :(得分: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
}
答案 1 :(得分: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
}
答案 2 :(得分: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
}
}
答案 3 :(得分: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
<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';
}
?>