我可以指定在html表单中选择单选按钮时希望运行的php代码块吗?
例如,我有许多用户可以填写的字段,但我希望PHP代码根据他们选择的单选按钮的不同而显示不同的内容我可以指定在html表单中选择单选按钮时希望运行的php代码块吗?,php,html,forms,radio-button,submit,Php,Html,Forms,Radio Button,Submit,例如,我有许多用户可以填写的字段,但我希望PHP代码根据他们选择的单选按钮的不同而显示不同的内容 <html> <body> <form action="citation.php" method="post"> <table width="100%" border="0"> <tr> <td>Name</td> <td><label for="name2"><
<html>
<body>
<form action="citation.php" method="post">
<table width="100%" border="0">
<tr>
<td>Name</td>
<td><label for="name2"></label>
<input type="text" name="name" id="name2"></td>
</tr>
<tr>
<td>Number</td>
<td><label for="number"></label>
<input type="text" name="number" id="number"></td>
</tr>
<tr>
<td>Email</td>
<td><label for="email"></label>
<input type="text" name="email" id="email"></td>
</tr>
<tr>
<td>Address</td>
<td><label for="address"></label>
<input type="text" name="address" id="address"></td>
</tr>
<tr>
<td>web</td>
<td><label for="web"></label>
<input type="text" name="web" id="web"></td>
</tr>
<tr>
<td><p>
<label>
<input type="radio" name="RadioGroup1" value="ex1" id="RadioGroup1_0">
ex1</label>
<br>
<label>
<input type="radio" name="RadioGroup1" value="ex2" id="RadioGroup1_1">
ex2</label>
<br>
<label>
<input type="radio" name="RadioGroup1" value="ex3" id="RadioGroup1_2">
ex3</label>
<br>
</p></td>
<td><input type="submit" name="click me" id="click me" value="Submit"></td>
</tr>
</table>
<p> </p>
</form>
</body>
</html>
名称
数
电子邮件
地址
网状物
例1
例2
例3
因此,如果他们选择第一个单选按钮并点击submit,则会显示出与选择第二个单选按钮不同的内容。当您进行标准表单提交时,表单字段的
名称字段会自动作为POST参数传递给php,即在$\u POST
中。如果字段有name=“test”
和value=“valueTest”
,那么在php层,您将以$\u POST['test']
的形式获得该值。以下是如何处理您的特殊情况的示例:
<?php
switch ($_POST['RadioGroup1']) {
case 'ex1':
// handle ex1 case
break;
case 'ex2':
// handle ex2 case
break;
case 'ex3':
// handle ex3 case
break;
default:
// handle cases here where RadioGroup1 has invalid value
break;
}
?>
这是非常基本的PHP
<?php
if($_POST['RadioGroup1']=='ex1'){
?>
You hit the first radio button, now he's hurt!
<?
}elseif($_POST['RadioGroup1']=='ex2'){
?>
You hurted the feelings of the second radio btn
<?
}elseif($_POST['RadioGroup1']=='ex3'){
?>
The tird radio button was clicked by you
<?
}
?>
你按了第一个单选按钮,现在他受伤了!
你伤害了第二台btn的感情
您单击了tird单选按钮
甚至更好:
<?php
switch($_POST['RadioGroup1'){
case 'ext1':
?>
You hit the first radio button, now he's hurt!
<?
break;
case 'ext2':
?>
You hurted the feelings of the second radio btn
<?
break;
case 'ext3':
?>
The tird radio button was clicked by you
<?
break;
default: die('wrong')
}
}
?>
你按了第一个单选按钮,现在他受伤了!
你伤害了第二台btn的感情
您单击了tird单选按钮
除了在PHP中粘贴HTML,您还可以包括以下文件:
<?php
switch($_POST['RadioGroup1'){
case 'ext1':
include('form/1clicked.php')
break;
....
}
}
?>
或者让他们针对每个选项转到不同的页面会更好吗?我的目标是创造一个小小的引用机器。所以对于MLA风格,他们只需要选择引用的类型,因此需要不同的选项。
if(isset($_POST['firstradio']))
{
echo "Content for First Radio";
}
elseif(isset($_POST['secondradio']))
{
echo "Content for Second Radio";
}
else
{
echo "Something";
}