dmanexe
dmanexe

Reputation: 1044

Why does my form value not appear in $_REQUEST?

I have this code from an HTML form:

<select name="history12">
    <option value="Gov/Econ">Government &amp; Economics</option>
    <option value="AP Gov/Econ">AP Government &amp; Economics</option>
</select>

...and this code, in a mailer form:

$history12 = $_REQUEST['history12'] ;

However, when I try to echo() $history12, it always returns blank. I can't figure out what I'm doing wrong since other inputs work fine (text and radio) but it seems like it's bonking on selects.

Upvotes: 0

Views: 3618

Answers (6)

Narcissus
Narcissus

Reputation: 3194

Have you ensured that you don't accidentally have another form element after the one above with the same name?

A couple of times I've had, for example "history12" as a dropdown but then a hidden field after it with the same name and no value. That will overwrite the selection.

Upvotes: 1

Lance Kidwell
Lance Kidwell

Reputation: 905

When I run this:

<form action='me.php' method='POST'>
<select name="history12">
    <option value="Gov/Econ">Government &amp; Economics</option>
    <option value="AP Gov/Econ">AP Government &amp; Economics</option>
</select>
<input type='submit' name='submit' value='lksjdflk' />
</form>
<pre>
<?php 

var_dump($_REQUEST);

?>
</pre>

i get this:

array(2) {
  ["history12"]=>
  string(11) "AP Gov/Econ"
  ["submit"]=>
  string(8) "lksjdflk"
}

You need to post the html around your form and the code that's receiving it server-side.

also, try changing the values from "Gov/Econ" to "Gov Econ" and see what you get. You might be running some code that's processing the $_REQUEST and doing something funky with regular expressions or something and stripping your value.

Upvotes: 0

backslash17
backslash17

Reputation: 5390

If have repeated your code in a clean form and it works fine. Your problem has to be somewhere else.

Take this code and begin to add the other components, you can test wich one is giving you a problem.

<form action="test.php" method="post">
<select name="history12">
    <option value="Gov/Econ">Government &amp; Economics</option>
    <option value="AP Gov/Econ">AP Government &amp; Economics</option>
</select>
<input name="send" type="submit" value="send" />
</form>
<?php

if(isset($_POST['history12'])) {
    $history12 = $_REQUEST['history12'] ;
    echo $history12;
}
?>

Upvotes: 1

&#211;lafur Waage
&#211;lafur Waage

Reputation: 70001

A few things to check

  • Is there a form element around it
  • Does the form element use POST
  • Do you get anything from print_r($_REQUEST);
  • And as a last resort, do you see the value anywhere in get_defined_vars()

Upvotes: 4

harto
harto

Reputation: 90513

Perhaps you could try outputting the entire $_REQUEST variable, to ensure everything you're expecting is showing up. That might at least indicate if the 'history12' key is set.

print_r($_REQUEST);

Upvotes: 5

lsl
lsl

Reputation: 4419

Make sure something is selected when you submit the form.

Something is selected by default, so it is more likely you are wiping the variable somewhere or closing your form in html before putting the select.

You could also have some malformed html elsewhere doing something funky to the form.

Upvotes: 0

Related Questions