-1

I have a foreach statement that echos radio buttons.

All those radio buttons have same name.

When I want to get the clicked ones, I use $_POST['radio_name']

But I got an error (it can't find the radio name)

This my code :

<form method="post">
        <div class="repas-inside-bloc  breakfast-bloc" id="brkID">
            <?php 
                foreach($breakfast_array as $brk){
                    echo '<label for="'.$brk['id_plat'].'" class="plan-meal-box">'.$brk['titre_plat'].'</label><input name="brk_check" type="radio" id="'.$brk['id_plat'].'" value="'.$brk['titre_plat'].'">';
                }

            ?>
        </div>

How to get values of every radio button clicked ? $_POST['brk_check'] doesn't work

Sushi
  • 646
  • 1
  • 13
  • 31
  • `print_r($_POST)` to see what gets posted – Thamilhan Jan 21 '17 at 14:44
  • First, to help, we need to see the *rendered HTML*, not just the PHP. Second, are you sure a radio button is checked? Third, is the form posting at all? Do you get OTHER form values from `$_POST`? Lastly, a `var_dump( $_POST );` will reveal ALL the radios that are checked. – random_user_name Jan 21 '17 at 14:46
  • @cale_b There is a value "stored" on the $_POST i see it when i print_r($_POST) this value comes from a first web page (page 1 : click on a cars brand name (this is the value on post) , page 2 ; depending on the brand, many radio buttons for cars of this brand) – Sushi Jan 21 '17 at 14:53
  • `name="brk_check"` that needs to be treated as an array for one thing `name="brk_check[]"` and we don't know how it's treated thereafter. – Funk Forty Niner Jan 21 '17 at 14:59

2 Answers2

-1

The idea of radio buttons is they are grouped by their name attribute. If all radio buttons have same name, only one can be clicked at a time.

Therefore, the server value you receive as $_POST['brk_check'] is value of radio button that was clicked, which can only be one value at any given time.

If you want to receive multiple values, you have to name radio buttons differently. But with this usage, if you want to allow multiple selections, you should probably type='checkbox' instead of radio buttons. Here is an answer of how to read multiple checkbox values.

Community
  • 1
  • 1
Marko Gresak
  • 7,950
  • 5
  • 40
  • 46
  • Thank you for you answer, but my problem is not if I want send on or many values, the problem is that I have a first page with a form, so by clicking on this form I have a value on $_POST and this action take me on another page, this new page (that contains those radio buttons) is a form too, so maybe the problem is here – Sushi Jan 21 '17 at 16:47
  • Sorry, but I don't understand what you mean. If you want to redirect to specific page, use `
    `. From there create `next_page.php` and inside the file, use `$_POST` inside the file. If you mean something else, you'll have to write it in a more understandable manner.
    – Marko Gresak Jan 21 '17 at 16:56
  • Sorry for my english. No I already did what you've just explained (first page with a form, when submitting, it redirects me on a second page) Now in this second page i have a form too, the post on the second page doesn't work and I don't understand why – Sushi Jan 21 '17 at 16:58
  • Check that name of radio buttons rendered on page is what you've set and what you're then trying to use in `$_POST`. According to your code, this should be `$_POST['brk_check']`. Make sure you don't have any typos. If that doesn't solve it, it would be best to provide full code, it's hard to figure out what's wrong when seeing only one half of the code. – Marko Gresak Jan 21 '17 at 19:40
-2

You can try this:

$_REQUEST['brk_check'];
FelixSFD
  • 6,052
  • 10
  • 43
  • 117
Rohit Kumar
  • 438
  • 2
  • 6
  • 16
  • 1
    This does not provide an answer to the question. Once you have sufficient [reputation](http://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](http://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](http://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/low-quality-posts/14958711) – yivi Jan 21 '17 at 18:45
  • @yivi A code-only answer might not be a good one, but it's still an answer. I would recommend you this post about the LQPRQ: [You're doing it wrong: A plea for sanity in the Low Quality Posts queue](http://meta.stackoverflow.com/questions/287563/youre-doing-it-wrong-a-plea-for-sanity-in-the-low-quality-posts-queue) – FelixSFD Jan 21 '17 at 18:48
  • Thanks @FelixSFD, I'll read it right now! But my flag wasn't about being code only. I thought that with the information provided in the answer this was very low quality. If the user thought that the problem was related about the form being sent by GET and not POST, I think it should say so. I feel that as it stands it neither answer the question (why it might not find this value in the $_POST superglobal) nor elaborates in the hypothesis that in fact it's a GET request. Someone less informed could even believe that this is "right" and accesing `_POST` is "wrong". Off to read that link. :) – yivi Jan 21 '17 at 18:55