Forum Moderators: coopster

Message Too Old, No Replies

PHP 4.3.4 upgrade ... need help with drop downs

I upgraded from PHP 4.1.2 to 4.3.4 and need to retrieve SELECT box data

         

bubone2

10:24 pm on Nov 5, 2004 (gmt 0)

10+ Year Member



Hello,

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

dmorison

10:29 pm on Nov 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Easiest way to crack this sort of problem is to do a print_r($_POST); to see exactly what is in $_POST.

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.

coopster

10:34 pm on Nov 5, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Quite clear, and right on. That should work. What is or isn't happening?

jatar_k

10:36 pm on Nov 5, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I am guessing it is the html that is the problem

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

bubone2

10:50 pm on Nov 5, 2004 (gmt 0)

10+ Year Member



jatar_k,

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]

coopster

10:54 pm on Nov 5, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



True, you should have values, but even without the values the code will work in non-strict mode. But, there is something else funny here. You may end up posting a bit of code...

bubone2

10:55 pm on Nov 5, 2004 (gmt 0)

10+ Year Member



coopster,

Please elaborate on what sounds funny to you, so that I know what to post for you.

I am at this moment troubleshooting the code by implementing the print_r($_POST); as was suggested.

Josh

dmorison

11:00 pm on Nov 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You haven't inadvertantly closed your form (with </form>) before the <select>, have you?

coopster

11:03 pm on Nov 5, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



As jatar_k stated, you seem to have something else, possibly in the html or otherwise not quite right. The way you are trying to read the SELECT name variable should work. As a matter of fact, I tested it and it does indeed work. However, my test was quite simple. Here is the code...

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



Also, as dcrombie pointed out, there are quick workarounds, including the for loop described or another quick work around without the loop is just to extract the superglobal:
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.

dmorison

11:17 pm on Nov 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



including the for loop described or another quick work around without the loop is just to extract the superglobal: extract($_POST);

I really should read the PHP manual one of these days :o)

bubone2

11:32 pm on Nov 5, 2004 (gmt 0)

10+ Year Member



Ok, think I have a working dumbed down version for you to look at.

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

bubone2

11:08 pm on Nov 7, 2004 (gmt 0)

10+ Year Member



Thanks to all who shared some input on this issue. As it turns out, I had a simple naming conflict in my $_POST statement.

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

coopster

6:34 pm on Nov 8, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Are you confused about Using Register Globals [php.net], or how you can setup a quick workaround for the PHP configuration directive using Apache?

If it is the former, what specifically isn't quite clicking? We'll do our best to clear the confusion.

bubone2

10:47 pm on Nov 8, 2004 (gmt 0)

10+ Year Member



I am fairly new to PHP (about 1 yr of use), so I don't know all of the ins and outs of the language (obviously).

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

coopster

12:13 am on Nov 9, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member




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.