简体   繁体   中英

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.

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);

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()

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;
}
?>

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM