Page 1 of 1
[b]Trouble with Accessing Form Variables[/b]
Posted: Tue Jun 18, 2002 12:40 pm
by Eugene
I am building a search engine application that uses an Option Form in order to get the user's input. A PHP script is called once the search criterion is chosen. I try to use the following command in the PHP script:
Code: Select all
$music_key = $HTTP_POST_VARSї"music_key"];
but I get an error message:
Notice: Undefined index: music_key in C:\EugPersonnel\www\search_results_V1_3.php on line 8
The code for the Option Form looks like this:
<FORM METHOD=POST ACTION= "search_results_V1_3.php">
<B>Music type:</B><BR>
<SELECT >
<OPTION SELECTED NAME="music_key" VALUE=0> All
<OPTION NAME="music_key" VALUE=1> Hip-Hop
<OPTION NAME="music_key" VALUE=2> Rap
<OPTION NAME="music_key" VALUE=3> R&B
<OPTION NAME="music_key" VALUE=4> Top Ten
<OPTION NAME="music_key" VALUE=5> Soka
</SELECT>
<P><INPUT TYPE="submit" VALUE="Submit">
I look in a PHP book, but I can't find what I am doing wrong!!!
Does anybody know what the source of the problem is????????
I have register_globals to OFF and track_vars is always enabled by default.
I even tried
Code: Select all
$music_key = $_POSTї'music_key'];
but it doesnt work either.
I suspect the error to be in the actual HTML code!!!! Is that possible???
Thanks
Eugene

Posted: Tue Jun 18, 2002 1:12 pm
by Zmodem
you need a </option> closing tag for all your <option> tags

like this:
<OPTION SELECTED NAME="music_key" VALUE=0> All </option>
<OPTION NAME="music_key" VALUE=1> Hip-Hop</option>
<OPTION NAME="music_key" VALUE=2> Rap</option>
<OPTION NAME="music_key" VALUE=3> R&B</option>
<OPTION NAME="music_key" VALUE=4> Top Ten</option>
<OPTION NAME="music_key" VALUE=5> Soka</option>
Posted: Tue Jun 18, 2002 1:24 pm
by twigletmac
you need a </option> closing tag for all your <option> tags
In XHTML not in HTML.
I suspect the error to be in the actual HTML code!!!! Is that possible???
Basically if the problem was in the HTML code then the page would not display as intended and you wouldn't get PHP errors. So the error is in your PHP code.
When you try and access the $_POST variables is it on the same page as the form, if so do you do:
Code: Select all
if (isset($_POST)) {
// code if form has been posted
} else {
// display form
}
Mac
REALLY ODD BEHAVIOUR FOR POST
Posted: Tue Jun 18, 2002 2:16 pm
by Eugene
Mac
I changed your code to
Code: Select all
if (!isset($_POSTї'music_key']))
{
//POST not set, display form
}
else
{
//code if form was posted
}
I did that because just
always returns TRUE, even when I open a new explorer window and reconect to the site !!!!! (I guess there is always some kind of garbage data in POST)
However,
Code: Select all
(isset($_POSTї'music_key']))
for some obscure reason ALWAYS returns FALSE, even after I actually chose a value and click Submit!!!!! The code thus just remains in the first IF statement.
With the changes you suggested I
did put everything on the same file, and the Submit action just calls itself (the same php file). Before, as I first stated the problem, I was having a separate HTML file with the form call a php file.
So, as it still doesnt work, I am wondering if there is not an error in the way my IIS serves HTTP Posts, or if there is a config in php.ini that is not correct????????
thanks
Eugene
Posted: Tue Jun 18, 2002 2:21 pm
by twigletmac
Have you tried doing,
Code: Select all
echo '<pre>';
print_r($_POST);
echo '</pre>';
to see what's in the $_POST array.
I have to run PHP 4.2 on IIS at work and have never had the same problem.
One thing I noticed - you do have a closing </form> tag don't you? Perhaps it could be an HTML problem after all

.
Mac
Posted: Tue Jun 18, 2002 2:33 pm
by Eugene
I dont have one!

I added it , but it doesn change anything.
However, I have the code building the Form displayed by using one big
Code: Select all
echo "<html><body>....<form ..>....</form> </html>";
statement. Can that cause a problem????
thankx a lot
eug
Posted: Tue Jun 18, 2002 2:38 pm
by Eugene
Mac
I tried adding the code u suggested, and what it shows all the time is:
Array
(
)
Looks like nothing ever enters that array????
Eug
Posted: Tue Jun 18, 2002 3:53 pm
by hob_goblin
if you are echo'ing it, you might have quote problems..and one more thing....you should change
<FORM METHOD=POST ...
to
<form method=\"post\" ...
Posted: Tue Jun 18, 2002 4:04 pm
by DSM
You need to name the <select> tag not the <option> tag the "<option>" attribute "value" gives the "<select>" form field its value.
Code: Select all
<select name = music_key>
<OPTION SELECTED VALUE=0> All </option>
<OPTION VALUE=1> Hip-Hop</option>
<OPTION VALUE=2> Rap</option>
<OPTION VALUE=3> R&B</option>
<OPTION VALUE=4> Top Ten</option>
<OPTION VALUE=5> Soka</option>
</select>
*only need </option> for XHTML
Try it this way.
Posted: Tue Jun 18, 2002 5:53 pm
by Eugene
Thanks Goblin, I double checked for the escape characters for the quotes, it fine.
Thanxs DSM, I am not next to my computer now so I cant check out our advice, but I
think that is it. Definitelly think it is the HTML, cause the php looks good.
And DSM, can I copy and use ur pic of Che????
Posted: Wed Jun 19, 2002 12:40 am
by Zmodem
An awesome HTML reference can be found at
http://www.htmlhelp.com
It lists all HTML tags, and all valid attributes for them. It's not a tutorial site with lots of articles, just reference material for those in the know
Take a look at it next time you use a <select> It will help ya out a lot
good luck on your project
Posted: Wed Jun 19, 2002 1:34 am
by twigletmac
Yep, DSM got it straight on the head, there is no name attribute for the <option> tag so it was just ignored.
A couple of things to do next time you think you have an HTML error on a form:
- When the page has loaded go to View -> Source to see what the browser thinks it's getting.
- Try adding other fields to the form like text inputs, radio buttons etc to see if anything is being sent through as it'll help you narrow down what exactly the problem is.
Mac
Posted: Wed Jun 19, 2002 10:07 am
by Eugene
THANKS EVERYBODY
Works perfectly now
Eugene
Posted: Wed Jun 19, 2002 10:13 am
by DSM
thats great glad I can help.