2017-09-05 4 views
0

Ich habe einen Code. Irgendwie ist es nur möglich, das letzte verborgene Eingabe-Namensfeld anstelle des anderen zu wählen. Ich habe auch versucht, wenn und sonst, aber nichts wird angezeigt. Bitte beraten.Wie unterscheidet man Namen in PHP

Ohne die sonst wenn Fälle Szenario:

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> 

HINWEIS: Bitte nehmen Sie das Ladegerät mit Ihrem iPad Trade-in oder eine Ersatzgebühr wird vom Angebot abgezogen

   <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; 

Mit den else if-Szenarien:

HTML-Code:

  <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-Code:

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

Nichts Arbeit. Bitte empfohlen

+2

Sie müssen möglicherweise durch den Code gehen und Ihre Rechtschreibung überprüfen, da Sie "Bedingung" hier falsch geschrieben: $ _POST [ "conditition"] – slevy1

Antwort

0

funktioniert gut für mich. Ich weiß nicht, ob deine Formularmethode Post ist oder nicht oder deine Aktions-URL auf deine Skript-URL eingestellt ist oder wenn dein Skript auf derselben Seite ist, stelle sicher, dass du es entsprechend platzierst.

<!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

I POST verwenden und es in einer anderen senden Seite namens "offer.php". Auf der Angebotsseite erhalte ich die Informationen über _POST. Allerdings habe ich versucht, es zu wiederholen, aber es funktioniert nicht – Luke

+0

Danke, es funktioniert – Luke

+0

Gut zu hören. :-) – Mobi

0

Ich glaube, das ist, was Sie zu tun versuchen?

EDIT:

<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

Es funktioniert nicht. Das einzige, was ich von dem ohne die if-and-else-Anweisung bekommen kann, ist 10. Es scheint, als würde es nicht die 7.50 bekommen. Übrigens ist es zwei Seiten Code. Eine in HTML mit Formularfeld und POST, die es an die Seite offer.php sendet. Die Seite offer.php verwendet den von mir geposteten PHP-Code – Luke

+0

Nun, wenn Sie mit PHP-Code arbeiten, ist es das Beste, wenn Sie alle Ihre Dokumente als .php anstatt als .html und dann als .php. Ich bin sicher, es ist keine große Sache, aber es würde das Leben viel einfacher machen. – JeanPaul98

+0

Also, was Sie versuchen zu tun ist:
Wenn der Benutzer wählt gut, Sie wollen es Echo 7.50 sonst, wenn der Benutzer wählt makellos wollen Sie es Echo 10? – JeanPaul98

Verwandte Themen