如何在PHP中区分名称

时间:2017-09-05 23:06:29

标签: php if-statement

我有一个代码。不知何故,它只能选择最后一个隐藏的输入名称字段而不是另一个。我也试过使用if和else但没有显示任何内容。请指教。

没有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" 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 个答案:

答案 0 :(得分:0)

对我来说很好。不知道您的表单方法是否发布,或者您的操作网址是否设置为您的脚本网址,或者您的脚本位于同一页面上,请确保相应地放置它。

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

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

答案 1 :(得分: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
    }
}
?>