我有几个单选按钮,a和B。两者都有四个单选按钮。我的代码现在与所选单选按钮的值相呼应,但这不是我想要的。我想让它检查选择了哪个按钮,然后回显产品名称和价格。
<!DOCTYPE html>
<html>
<head>
<title>Ordering System</title>
<!-- include css file here-->
<link rel="stylesheet" href="css/style.css"/>
</head>
<body>
<?php
//prices of Radio A
$A1 = 10;
$A2 = 20;
$A3 = 30;
$A4 = 50;
//prices of Radio B
$B1 = 20;
$B2 = 30;
$B3 = 40;
$B4 = 50;
?>
<div class="container">
<div class="main">
<h2>A or B</h2><hr/>
<form method="post" action="form.php">
<hr/>
<!---------Radio Button's starts here------>
<label class="heading">A:</label><br/>
<input type="radio" name="A" value="A1">Name Of Product A1<br/>
<input type="radio" name="A" value="A2">Name Of Product A2<br/>
<input type="radio" name="A" value="A3">Name Of Product A3<br/>
<input type="radio" name="A" value="A4">Name Of Product A4<br/><br/>
<label class="heading">B:</label><br/>
<input type="radio" name="B" value="B1">Name Of Product B1<br/>
<input type="radio" name="B" value="B2">Name Of Product B2<br/>
<input type="radio" name="B" value="B3">Name Of Product B3<br/>
<input type="radio" name="B" value="B4">Name Of Product B4<br/><br/>
<?php
if (isset($_POST['submit'])) {
if(isset($_POST['A']))
{
echo "First selection:<b> ".$_POST['A']."</b> <br>" ;
}
else{ echo "<span>Please choose.</span>";}
}
if (isset($_POST['submit'])) {
if(isset($_POST['B']))
{
echo "Second Selection:<b> ".$_POST['B']."</b>";
}
else{ echo "<span>Please choose.</span>";}
}
$radio = $_POST['A'];
if ($radio == 'A1') {
echo $A1}
?>
<hr/>
<input type="submit" name="submit" value="Order" />
</form>
</div>
</div>
</body>
</html>
当然,当我遇到这个问题时,我在谷歌上搜索了一下,发现了这个。但当我像上面的一样实现它时,这给了我一个错误
错误:(!)分析错误:语法错误,意外的"}",应为"、"或";"在C:''wamp''www''OK''form.php中,第78行
<?php
$radio = $_POST['RadioName'];
if ($radio == 'Value') {
echo "Print The Price Here"}
?>
然后我发现:但这给了我错误:(!)分析错误:语法错误,意外的"}",应为"、"或";"在C:''wamp''www''OK''form.php中,第81行
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
if ($_POST['RadioName'] == "Value")
{
echo ""
}
else if ($_POST['RadioName'] == "Value2")
{
echo ""
}
}
?>
当像一样实现时
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
if ($_POST['A'] == "A1")
{
echo $A1
}
else if ($_POST['A'] == "A2")
{
echo $A2
}
}
?>
还有其他解决方案吗?或者,这些解决方案中的任何一个都可以调整,使其发挥作用吗?
注意单选选项While在一个拥有相同名称但多个值的组中,并且在$_post数组中只有名称和值可用
尝试使用像checked
或selected
这样的子类,使用jquery。