Search code examples
phpgetquery-string

How do I get all GET values from a URL? (Missing Values)


I am looping through GET values and only getting 13 values, not matter what URL I submit. Also, it is not getting the values in sequential order...

When I loop through I only get 13 values, as well as when I use a var_dump on $_GET itself; even though there are many more values to retrieve.

Here is the URL:

website.com/Questionaire.php?SurveyName=TV%20Quiz&SurveyType=MultipleChoice&Q1=Choose%20a%20character%20off%20of%20Happy%20Days?&A1=Benny%20the%20bull&A2=The%20Fonz&A3=Jack%20Cracker&Q3=Favorite%20Friends%20character?&A1=Ross&A2=Monica&A4=Joey&A5=Rachel&A6=Chandler&A7=Phoebe&Q8=Favorite%20Nickelodeon%20show?&A1=Hey%20Arnold!&A2=Rugrats&A8=Ahhhh!%20Real%20Montsters

Here are my results:

SurveyName: TV Quiz

SurveyType: MultipleChoice

Q1: Choose a character off of Happy Days?

A1: Hey Arnold!

A2: Rugrats

A3: Jack Cracker

Q3: Favorite Friends character?

A4: Joey

A5: Rachel

A6: Chandler

A7: Phoebe

Q8: Favorite Nickelodeon show?

A8: Ahhhh! Real Montsters

As you can see the results are not in sequential order and some values are even missing.

var_dump($_GET);

foreach ($_GET as $key => $value) { 
    echo $key.": ".$value."<br/>\n";
}

I expect these results:

SurveyName=TV Quiz

SurveyType=MultipleChoice

Q1=Choose a character off of Happy Days?

A1=Benny the bull //<- missed

A2=The Fonz //<- missed

A3=Jack Cracker

Q3=Favorite Friends character?

A1=Ross //<- missed

A2=Monica //<- missed

A4=Joey

A5=Rachel

A6=Chandler

A7=Phoebe

Q8=Favorite Nickelodeon show?

A1=Hey Arnold!

A2=Rugrats

A8=Ahhhh! Real Montsters

Solution

  • You cannot have identical parameter names in your query string, otherwise the last value will overwrite the previous ones. You need to have unique answer names or you will lose data. You can imagine PHP adding the parameters to $_GET with the following pseudo-code:

    foreach($param as $key=>$val) {
        $_GET[$key] = $val;
    }
    

    Because of this, parameters appear in the order in which they first show up in the request. So the query string ?A=1&B=2&A=3&C=4 will have A appear first, then B, and finally C. The last value for an identical parameter is the one used, so we get the following $_GET result:

    array(
        'A'=>3,
        'B'=>2,
        'C'=>4
    );
    

    Consider adding the question identifier as a prefix for each answer. For example, instead of A1 do Q1A1 and Q2A1. This will ensure that your data is not overwritten.