PHP检查单选按钮,然后输出名称和价格

时间:2014-10-20 09:17:10

标签: php html radio-button

我有几个单选按钮,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>
当然,当我遇到这个问题时,我用Google搜索,我发现了这一点。但是当我像上面那样实现它时,这给了我一个错误

错误: (!)解析错误:语法错误,意外&#39;}&#39;,期待&#39;,&#39;或&#39;;&#39;在第78行的C:\ wamp \ www \ OK \ form.php

<?php
    $radio = $_POST['RadioName'];
    if ($radio == 'Value') {
    echo "Print The Price Here"}
?>

然后我发现:但这给了我错误: (!)解析错误:语法错误,意外&#39;}&#39;,期待&#39;,&#39;或&#39;;&#39;在第81行的C:\ wamp \ www \ OK \ form.php

<?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
    }
}
?>

还有其他解决方案吗?或者可以调整这些解决方案中的任何一个以便它们起作用吗?

2 个答案:

答案 0 :(得分:0)

了解无线电选项虽然在一个拥有相同名称但多个值的组中,并且在$ _post数组中只有名称和值可用

答案 1 :(得分:0)

尝试使用像checkedselected这样的子类,使用jquery。