2017-09-05 72 views
0

我有一個代碼。不知何故,它只能夠選擇最後一個隱藏的輸入名稱字段,而不是另一個。我也嘗試使用if和else,但沒有顯示任何內容。請指教。如何區分PHP中的名稱

沒有,如果其他情況下的場景:

HTML:

  <div class="tab-label"> 
         <input type="radio" id="ldktech_product_good" name="conditition" value="good" > 
         <input type="hidden" id="ldktech_product_price_good" name="price" value="7.50"> 
         <label for="ldktech_product_good">Good</label> 

注:請在您的iPad以舊換新,或更換費用充電器將從報價 中扣除

   <div class="tab-label"> 
         <input type="radio" id="ldktech_product_flawless" name="conditition" value="flawless" > 
         <input type="hidden" id="ldktech_product_price_flawless" name="price" value="10"> 
         <label for="ldktech_product_flawless">Flawless</label> 

PHP:

$condition = $_POST["conditition"]; 
$price = $_POST["price"]; 
echo $price; 
echo "<br>"; 
echo $condition; 

通過的if else場景:

HTML代碼:

  <div class="tab-label"> 
         <input type="radio" id="ldktech_product_good" name="conditition" value="good" > 
         <input type="hidden" id="ldktech_product_price_good" name="price-good" value="7.50"> 
         <label for="ldktech_product_good">Good</label> 


       <div class="tab-label"> 
         <input type="radio" id="ldktech_product_flawless" name="conditition" value="flawless" > 
         <input type="hidden" id="ldktech_product_price_flawless" name="price-flawless" value="10"> 
         <label for="ldktech_product_flawless">Flawless</label> 

PHP代碼:

$condition = $_POST["conditition"]; 
if($condition == "good"){ 
$price = $_POST["price-good"];} 
else if ($condition == "flawless"){ 
$price = $_POST["price-flawless"];} 
echo $price; 
echo "<br>"; 
echo $condition; 

沒有工作。請告知

+2

您可能需要通過你的代碼,並檢查拼寫,因爲你拼錯的「狀態」在這裏:$ _ POST [「conditition」] – slevy1

回答

0

適合我。不知道你的表單方法是否發佈或者你的動作url被設置爲你的腳本url,或者你的腳本在同一頁面上,請確保你相應地放置它。

<!DOCTYPE html> 
 
<html> 
 
<head> 
 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
 
<title>PhpFiddle Initial Code</title> 
 

 
</head> 
 

 
<body> 
 
<div style="margin: 30px 10%;"> 
 
<h3>My form</h3> 
 
<form id="myform" name="myform" method="post"> 
 

 
\t <div class="tab-label"> 
 
         <input type="radio" id="ldktech_product_good" name="conditition" value="good" > 
 
         <input type="hidden" id="ldktech_product_price_good" name="price-good" value="7.50"> 
 
         <label for="ldktech_product_good">Good</label> 
 

 

 
       <div class="tab-label"> 
 
         <input type="radio" id="ldktech_product_flawless" name="conditition" value="flawless" > 
 
         <input type="hidden" id="ldktech_product_price_flawless" name="price-flawless" value="10"> 
 
         <label for="ldktech_product_flawless">Flawless</label> <br /><br /> 
 
    
 
\t <button id="mysubmit" type="submit">Submit</button><br /><br /> 
 

 
</form> 
 
</div> 
 

 
<?php 
 
if(isset($_POST["conditition"])){ 
 
    $condition = $_POST["conditition"]; 
 
    if($condition == "good"){ 
 
     $price = $_POST["price-good"]; 
 
    } 
 
    else if ($condition == "flawless"){ 
 
     $price = $_POST["price-flawless"]; 
 
    } 
 
echo $price; 
 
echo "<br>"; 
 
echo $condition; 
 
} 
 
?> 
 

 
</body> 
 
</html>

+0

我使用POST並將其發送給另一頁面名爲「offer.php」。在商品頁面上,我通過_POST檢索信息。但是,我試圖迴應它,但它不起作用 – Luke

+0

謝謝,它的工作 – Luke

+0

很高興聽到。 :-) – Mobi

0

我相信這是你想要做什麼?

編輯:

<form method="post"> 
    <div class="tab-label"> 
     <input type="radio" name="condition1" value="good"> 
     <input type="hidden" name="price1" value="7.50"> 
     <label for="ldktech_product_good">Good</label> 
    </div> 

    <div class="tab-label"> 
     <input type="radio" name="condition2" value="flawless"> 
     <input type="hidden" name="price2" value="10"> 
     <label for="ldktech_product_flawless">Flawless</label> 
    </div> 
    <input type="submit" name="submit" value="Get records"> 
</form> 
<?php 
if (isset($_POST['submit'])) { 
    if (isset($_POST['condition1']) && $_POST['condition1'] == "good") { 
     echo "You have selected :".$_POST['price1']; // Displaying Selected Value 
    } else if (isset($_POST['condition2']) && $_POST['condition2'] == "flawless") { 
     echo "You have selected :".$_POST['price2']; // Displaying Selected Value 
    } 
} 
?> 
+0

它不起作用。我可以從沒有if和else語句的人那裏得到唯一的東西是10.看起來它沒有得到7.50。順便說一下,這是兩個頁面的代碼。一個在表單字段和POST的HTML中,將它發送到offer.php頁面。 offer.php頁面使用我發佈的PHP代碼 – Luke

+0

那麼,如果你正在使用php代碼,那麼最好將所有文檔作爲.php而不是一個作爲.html,然後另一個作爲.php。我相信這沒什麼大不了的,但它會讓生活變得更容易。 – JeanPaul98

+0

所以你要做的是:
如果用戶選擇好,你希望它呼應,如果用戶選擇無瑕7.50否則你想讓它呼應10? – JeanPaul98

相關問題