2
foreach($rates as $item){    
    if ($item->disabled == false){
        echo '<input type="radio" name="rate" id="membership" required="yes" message="Please select your membership type."  value="' . $item->rate . '"><input type="hidden" name="membership" value="' . $item->membership . '" >';
    }
}

This is probably pretty simple, but the issue that I'm having is that I can't get $item->membership to submit with $item->rate. For example when somebody selects the '125' rate, it should also submit 'Student' or whatever. When I do submit the form though, it submits the last $item->membership in the series instead of the one that is tied to it's rate... Any help would be appreciated, I'm sure I'm overthinking this.

Thanks

4
  • 3
    All your hidden inputs will have the same name of membership. That's fine for a radio button, but not for other types of input. Commented Jul 26, 2013 at 17:22
  • What would the appropriate way be to submit $item->rate with the correct $item->membership then? Commented Jul 26, 2013 at 17:26
  • best way is to get the membership against the posted rate do not try to get in hidden field user can change the data of fields Commented Jul 26, 2013 at 17:29
  • @AustinSlominski - you could name the hidden field as 'membership_' . $item->rate, which means you'd be able to work out which field to use based on the rate submitted. But as dianuj says - it's probably best not to rely on the correct amount being passed back from the form, in case it's been altered. It's a little bit more work, but it's more secure to take the submitted rate and query the database again to get the right amount. Commented Jul 26, 2013 at 17:53

2 Answers 2

3

Because they all have the same name attribute, so they set the same variable multiple times and thus the last one is what you always get. Try giving all the hidden inputs unique names, so you can get the membership you want. HTML forms aren't smart enough to pick only the hidden input next to the radio button, they all get posted.

Sign up to request clarification or add additional context in comments.

Comments

1

When you submit an form, hidden field and radio button are independent elements regardless how you wrap them together.

Here is my solution to pass multiple variables for each radio button

view:

<form method="post" action="your_post_file.php" >
    <?php $i = 1;  foreach($rates as $item){?>
        <input type="radio" name="rates[]" value="<?php echo $i?>" /><?php echo $item->rate?>
        <input type="hidden" name="rate<?php echo $i?>" value="<?php echo $item->rate?>" />
        <input type="hidden" name="membership<?php echo $i?>" value="<?php echo $item->member_id?>" />
    <?php $i++;}?>

    <input type="submit" />
</form>

on the your_post_file.php

if(isset($_POST['rates'])){
            if(is_array($_POST['rates'])){

                $index = $_POST['rates'][0]; //return the index of selected radio button

                $rate =  $_POST['rate' . $index];
                $membership_id =  $_POST['membership' . $index];

                //print out the result
                var_dump($rate);
                var_dump($membership_id);

            }
        }

Bingo, you get what you want, even you want more the value for each rate, just add more hidden field with same given format, such as first name, last name, or anything else.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.