2014-10-20 22 views
-1

我有幾個單選按鈕,A和B.兩個都有四個單選按鈕。 我的代碼現在回顯所選單選按鈕的值,但這不是我想要的。我想要檢查哪個按鈕被選中,然後回顯產品的名稱和價格。PHP檢查單選按鈕,然後輸出名稱和價格

<!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了當我得到這個問題,我發現這一點。但是,當我像上面實現它時,這給我一個錯誤

錯誤: (!)解析錯誤:語法錯誤,意外'}',期待','或';'在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 
    } 
} 
?> 

是否有另一種解決方案?或者這些解決方案中的任何一種都可以調整,以便它們能夠工作?

+0

你在echo $ A1後缺少分號,它應該是echo $ A1;在線78 – 2014-10-20 09:44:25

回答

0

知道無線電選項雖然是一組自己與名稱相同,但多值,並在$ _POST數組只有名稱和值可

0

嘗試使用子類像checkedselected,使用jQuery。