Forum Moderators: coopster
Some of you may have seen a similar post to this by me on the Macintosh Webmaster forum board. This is a similar issue, but little different.
I upgraded from PHP 4.1.2 to 4.3.4 (Mac OS X 10.2.8), and then had to adjust all of my form scripts to retrieve the POST variables explicitly (because 4.3.4 requires this). I did this like the following:
<input type=text name=myinputbox>
<input type=submit name=submit value=submit>
-------
$myinputbox = $_POST[''myinputbox];
$submit = $_POST['submit'];
That works just fine, and the variable data is available for use elsewhere in the script. Radio buttons and textareas also work in this way.
However, my problem is that <SELECT> drop down menus apparently DO NOT work in this way. This renders my form useless without this ability. If you can post a reply code for retrieving the <SELECT> POST variable to the following code, I would appreciate it.
<SELECT name="MENU">
<option>option1</option>
<option>option2</option>
<option>option3</option>
</SELECT>
I assumed it would be this (but didn't work):
$MENU = $_POST['MENU'];
Hope I am clear with my explanation.
Thanks,
Josh
Also, a quick hack to made regular variables out of the super-globals when you've been bad in the past is to do something like this:
foreach($_POST as $k => $v)
{
$$k = $v;
}
... and do the same for $_GET, $_COOKIE as required.
there are no values
<SELECT name="MENU">
<option>option1</option>
<option>option2</option>
<option>option3</option>
</SELECT>
should be
<SELECT name="MENU">
<option value="option1">option1</option>
<option value="option2">option2</option>
<option value="option3">option3</option>
</SELECT>
it should then work as expected
That is a great observation, I apparently made a mistake on my post as you say by leaving out the value attribute.
However, I do assign the value attribute in my script. I just overlooked that when i was trying to make a quick example in the post.
And to be more clear, as coopster suggests, what happens is that the value for that <SELECT> variable isn't retrieved and isn't available to post to my MySQL database. So I get null values for that field in the db.
Josh
[edited by: bubone2 at 10:54 pm (utc) on Nov. 5, 2004]
<html><head><title>SELECT Test</title></head><body>
<h1>SELECT Test</h1>
<form method="post" action="<?php print $_SERVER['PHP_SELF'];?>">
<SELECT name="MENU">
<option>option1</option>
<option>option2</option>
<option>option3</option>
</SELECT>
<input type="submit" name="Submit" value="Submit" />
</form>
<?php
if (isset($_POST['Submit'])) {
print '<pre>'; print_r($_POST['MENU']); print '<pre>';
$MENU = $_POST['MENU'];
print "<br />$MENU<br />";
}
?>
</body></html>
extract($_POST);or better yet, without having to modify any code at all you can drop a per-directory (.htaccess) override file in the web root to turn register_globals on. Here is a thread that may come in handy (yeah, I know I already led you down this road in your other post, but I'll put it here again for future readers)...
Clarification of Global Variables OFF [webmasterworld.com]
All that said, however, read the links in the link above that take you to the PHP manual pages describing why you don't want register_globals on.
This represents one of the sections in question (because the two are basically carbon copies of each other), and my submit button section.
It looks a little wonky because of all of the \ for delimiting the quotes, as I tend to do one echo statement for the html section and keep all my html within that one statement. I only close that echo statement in the middle if I have another php section to code inline with the html (which I do several times in this example). Generally I try to do all the major php coding in a section above the html section. So that should explain the initial echo statement that you may have concern over. (It is a little more clear in a nice editor where you can see my use of tabbing :-( )
_____________________________________________
<?
//Initializing variables
$SUBMIT_FORM = $_POST['SUBMIT_FORM'];
$INTENT_TO_FOLLOWUP = $_POST['INTENT_TO_FOLLOWUP'];
//Submit Section
if($SUBMIT_FORM)
{ //here I implement some checks and input my mysql statements which is where I need to use the $INTENT_TO_FOLLOWUP variable that I retrieved
die(print_r($_POST));
}
// HTML Section
echo"
<hml>
<head><title>Form test.php</title></head>
<body>
<form method=\"post\" action=\"test.php\">
"; //notice I stopped the echo statement for the php section to come
$option_value = $_POST['INTENT_TO_FOLLOWUP'];
echo"
<select NAME=\"INTENT_TO_FOLLOWUP\">
<option>SELECT ONE:</option>
";
if($option_value == 'YES')
echo"<option name=\"YES\" VALUE=\"YES\" SELECTED>Yes</option>";
else
echo"<option name=\"YES\" VALUE=\"YES\">Yes</option>";
if($option_value == 'NO')
echo"<option name=\"NO\" VALUE=\"NO\" SELECTED>No</option>";
else
echo"<option name=\"NO\" VALUE=\"NO\">No</option>";
if($option_value == 'MAYBE')
echo"<option name=\"MAYBE\" VALUE=\"MAYBE\" SELECTED>Maybe</option>";
else
echo"<option name=\"MAYBE\" VALUE=\"MAYBE\">Maybe</option>";
echo"
</select>
<input type=\"submit\" name=\"SUBMIT_FORM\" value=\"Submit Form\">
</form>
</body>
</html>
";
?>
_______________________________________________
Also something that may seems a little weird is how I call the $_POST twice for the same variable. This stems from the fact that I had a need to retrieve the POST variable for a couple of form inputs before the upgrade. But shouldn't interfere with each other or anything, just means there is more than one copy of that variable out there.
Anyway, I checked this code before I posted it, and it worked. So, this should be a good example. Note that the print_r($_POST) does post the variable as it should be. So apparently it IS something else as you have said. But one thing to remember also when you give some some tips, is that this was a 100% working code before the PHP upgrade. Therefore, I it shouldn't be the MySQL code. So I have omitted it.
Josh
I appreciate posting your comments in trying to help me with this. :? Couldn't believe it when I found my mistake. :pulling_my_hair_out: (I wish that was an emoticon).
dmorison, thanks for pointing out the use of print_r($_POST), I didn't know you could print the $_POST vars in that way. Before, I have echoed each indiv. variable, which is annoying to code in a large form for instance.
Also, I would like coopster to elaborate please (maybe start another forum discussion) on what you were saying about .htaccess. Honestly, you lost me there. I have followed some links and read up on superglobals and the like on php.net. However, they were pretty vague in the details about the supers. So further discussion on any of those areas would be helpful to me.
Thanks again,
Josh
If it is the former, what specifically isn't quite clicking? We'll do our best to clear the confusion.
One point of confusion was with superglobals. I followed your link and several subsequent links from there and found a page that lists all of the superglobals. However, it wasn't very specific with most of them.
I know what a superglobal is in terms of scope, but what I don't know is the specifics of each predefined superglobal. I know about $_POST and $_COOKIES (mostly), but don't know much specifics about the others.
[us2.php.net...]
That page explains $_SERVER well with a listing of all of the possible parms, and what info each parm would return. I want to know that information about the other predefined superglobals. SO, I am looking for 1) parm listing for each super, 2) what info each parm will return, 3) possible suggestion on what each would be used for.
Also, I would like you to expand on your mentioning of adjusting the .htacess file is, what is can be used for, and how to go about using it. This is just an area that I have never explored before, and really don't know what it is that I don't know about it. :-)
So, because I don't understand the basics of this area, then I also don't understand the 'workarounds' that it can provide.
That's probably more confusing than my original post, but hope you can decipher it.
Thanks for all your help so far,
Josh
That page explains $_SERVER well with a listing of all of the possible parms, and what info each parm would return. I want to know that information about the other predefined superglobals. SO, I am looking for 1) parm listing for each super, 2) what info each parm will return, 3) possible suggestion on what each would be used for.
The other superglobals are present based upon whether or not you are settting $_COOKIES, using $_POST or $_GET methods in your forms and links, $_SESSIONs, etc. The $_SERVER variables are hit and miss, but you can find out what is available by having a look at them on your server. I posted a little superglobal snippet script in the latest Bag 'o Tricks [webmasterworld.com] thread.
Also, I would like you to expand on your mentioning of adjusting the .htacess file is, what is can be used for, and how to go about using it. This is just an area that I have never explored before, and really don't know what it is that I don't know about it. :-)Hope that helps!
Best place to learn .htaccess [httpd.apache.org] is from Apache. Have a read here and if you have questions about any of it, post in the Apache forum [webmasterworld.com] here at WebmasterWorld.