简体   繁体   English

使用单选按钮表单提交多个值

[英]submit multiple values with radio button form

Is this better then?那这样更好吗?

<form id="form1" name="form1" method="post" action="cart.php">
    <input name="size" type="radio" value="Small">Small<br>
    <input name="size" type="radio" value="Large">Large<br>
    <input name="size" type="radio" value="XXL">XXL<br>
    <input type="hidden" name="sizes" id="sizes" value=""/>
    <input type="hidden" name="pid" id="pid" value="85" />
    <input type="submit" name="button" value="Add To Cart"/></form>

Just a quick note that for retreiving the result I am using this code here:请注意,为了检索结果,我在这里使用此代码:

if(isset($_POST['sizes'])){
    $myvar = $_POST['sizes'];
    echo "Your Size:", $myvar ; 
}

With JavaScript & jQuery:使用 JavaScript 和 jQuery:

$("#form1 input[name='size']").click(function(){
    var size = $('input:radio[name=size]:checked').val();
    $("#form1 input[name='sizes']").val(size);
}); 

That should do the trick.这应该够了吧。

You seem to be calling the radio buttons 'form1' but you have the same name for the first hidden field.您似乎正在调用单选按钮“form1”,但第一个隐藏字段的名称相同。

I would suggest that the form is submitting this field and ignoring the radio buttons for this reason.我建议表单提交此字段并因此忽略单选按钮。

Looking at your revised code, surely you need:查看您修改后的代码,您肯定需要:

$myvar = $_POST['size'];

if ($myvar) {
    echo "Size is $myvar";
}

'sizes' is never getting set to a 'true' value. 'sizes' 永远不会设置为 'true' 值。

Note: the if ($myvar) works, in this instance, as effectively as the 'isset' statement but is simpler to code and read.注意:在这种情况下, if ($myvar)与“isset”语句一样有效,但更易于编码和阅读。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM