简体   繁体   中英

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:

$("#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.

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.

Note: the if ($myvar) works, in this instance, as effectively as the 'isset' statement but is simpler to code and read.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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