tags:

views:

385

answers:

6

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.

A: 

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.

jim
Something is selected by default, and even with something selected, no dice.
dmanexe
+5  A: 

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);
harto
+3  A: 

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()
Ólafur Waage
This was the biggest problem, and fixed it. Thanks!
dmanexe
+1  A: 

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;
}
?>
backslash17
A: 

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.

Lance Kidwell
+1  A: 

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.

Narcissus