从类而不是值获取post值

时间:2015-04-20 11:37:02

标签: php

我正在尝试从我的一个输入字段中获取一个帖子值,并且我想知道我是否可以使用类字段来获取值而不是从值字段中获取它的帖子。

这是我的HTML

<div class="form_group">
<form id="form1" name="paying" method="post" action="payment.php">
    <input id="get_200" type="radio" name="payment" value="200" ><label class="get_option_200" for="get_200"><span>$200</span></label><br> 
    <input id="get_300" type="radio" name="payment" value="300"><label class="get_option_300" for="get_300"><span>$300</span></label><br>
    <input class="get_other" id="other" type="text" name="other" value="Enter amount here">
</form>
</div>

我的payment.php

$payment = html_entity_decode(str_replace("'", "\'", $_POST["payment"]));

    switch ($payment) {
        case "200":
            echo "200";
            break;
        case "300":
            echo "300";
            break;
        default:
            echo "No payments made";
    }


    $other = html_entity_decode(str_replace("'", "\'", $_POST["other"]));

    if($other == '' || $other == 'Enter amount here'){
        echo "Nope";
    }else{
        echo "Yep";
    }


    print_r($payment);
    die();

如您所见,我有2个单选按钮,并且由于切换方法,我可以选择值字段中的正确单选按钮值。 但是因为文本输入值字段是文本“在此处输入金额”,我输入的金额不会通过payment.php进行,我只获得“输入金额”而不是我输入的金额< / p>

1 个答案:

答案 0 :(得分:3)

更改

<input class="get_other" id="other" type="text" name="other" value="Enter amount here">

使用:

<input class="get_other" id="other" type="text" name="other" placeholder="Enter amount here">

点击输入后,记下您的金额。

你可能会遇到IE8和IE9的问题,所以你使用这个棘手的标签:

onfocus="this.value = ''" onblur=" if(this.value = '') { value = 'Enter amount here'}"
相关问题