-2
   foreach ($graphEdge as $graphNode) {
    echo
      "<div class='form-check mb-3'>" .
        "<input type='radio' name='fb_profile' class='form-check-input' value='".$graphNode['name']."'>" .
        "<img class='mr-2' src='" . $graphNode['picture']['url'] . "'>" . 
        "<label class='form-check-label' for='fb_profile'>" . 
        $graphNode['name'] . '</label>' . 
      "</div>";

    echo "<input type='hidden' name='fb_access_token' value='" . $graphNode['access_token'] . "'>";
    echo "<input type='hidden' name='fb_id' value='" . $graphNode['id'] . "'>";
    }

As the title suggests, I have 3 hidden inputs named 'fb_id' that when I POST in a form will always retrieve the last hidden input yet fb_access_token will always POST correctly.

CBroe
  • 91,630
  • 14
  • 92
  • 150
Nazar Abubaker
  • 495
  • 3
  • 7
  • 17
  • The name should be unique. – Sandra Jul 25 '18 at 10:01
  • How is the browser/server supposed to differenciate the inputs if they are named the same ? use an array as stated by Nikita – ᴄʀᴏᴢᴇᴛ Jul 25 '18 at 10:01
  • This is expected behaviour, try using array of input names, like `name="fb_id[]"` – JustBaron Jul 25 '18 at 10:02
  • and the question is? As written it is just a statement of fact – jwenting Jul 25 '18 at 10:04
  • Note that this is expected behaviour **for PHP**. HTML GET or POST protocol has absolutely nothing against multiple parameters named the same way; but if you are in PHP, you will have to parse them manually (unless you bend to the PHP way and add `[]` to the name in the form.) – Amadan Jul 25 '18 at 10:05
  • I just noticed you have it in POST, not GET; so apologies because it's not quite a duplicate. Parsing POST by hand correctly is not really trivial. Still, it is not _impossible_. However, if you have access to the HTML source, doing what the majority suggests is likely the best and safest way to go about it. – Amadan Jul 25 '18 at 10:09
  • FYI: Your question title copy&pasted into Google verbatim lead to the duplicate (“HTML form with multiple hidden control elements of the same name”) right away. – CBroe Jul 25 '18 at 10:20
  • @CBroe: As I noted, it is not a duplicate; the question I originally linked is for GET (while this question is for POST); and the question you linked is not PHP-specific and merely ask if it is legal (yes, yes it is) without any mention of how to get the values out of the request. – Amadan Jul 25 '18 at 10:32
  • @Amadan GET or POST makes little difference, when the square bracket syntax is used - PHP will take care of the decoding in both cases. I am not advising to keep the parameter names as they were and taking on the job of parsing the input data oneself in the script, that would be rather nonsense IMHO. (If one needed to get data coming from an outside system handled properly in PHP, that might be an option. But in a system where you are in control of everything, I would not advise anything else but the [] syntax.) – CBroe Jul 25 '18 at 10:36
  • @CBroe: I agree, if you read what I commented, bending to the PHP way is the way to go. I just don't like closing as duplicate when it's... you know... not :) – Amadan Jul 25 '18 at 10:38
  • That will retrieve ALL fb_id which is not what I want. I want just the ID that the user selects. @CBroe – Nazar Abubaker Jul 25 '18 at 11:07
  • 2
    These are _hidden_ fields, so there _is_ no user selection. If you want the user to actively select something, then don’t use hidden fields for that to begin with, that does not make much sense. – CBroe Jul 25 '18 at 11:15
  • The user DOES select from a list of their Facebook Pages (notice the input type='radio'). When a user selects, I can then retrieve their Page Name, ID and Access Token. For some reason, I can post their Access Token perfectly fine to the dB but ID is only giving me the last one – Nazar Abubaker Jul 25 '18 at 11:17

1 Answers1

0

Ok. As I see from conversation, you want to get values, which depends on radio. You can do it this way:

foreach ($graphEdge as $graphNode) {
    echo
        "<div class='form-check mb-3'>" .
            "<input type='radio' name='fb_profile' class='form-check-input' value='".$graphNode['name']."'>" .
            "<img class='mr-2' src='" . $graphNode['picture']['url'] . "'>" . 
            "<label class='form-check-label' for='fb_profile'>" . 
    $graphNode['name'] . '</label>' . 
        "</div>";

   echo "<input type='hidden' name='fb_access_token[".$graphNode['name']."]' value='" . $graphNode['access_token'] . "'>";
   echo "<input type='hidden' name='fb_id[".$graphNode['name']."]' value='" . $graphNode['id'] . "'>";
}

And then you can get selected values by radio value like this (let's suppose your form sends POST request):

$profile = $_POST['fb_profile'];
$accessToken = $_POST['fb_access_token'][$profile];
$id = $_POST['fb_id'][$profile];
Nikita Leshchev
  • 1,784
  • 2
  • 14
  • 26