[b]Trouble with Accessing Form Variables[/b]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Eugene
Forum Newbie
Posts: 24
Joined: Tue Jun 18, 2002 12:40 pm
Location: Montreal

[b]Trouble with Accessing Form Variables[/b]

Post 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&#1111;'music_key'];
but it doesnt work either.
I suspect the error to be in the actual HTML code!!!! Is that possible???

Thanks
Eugene 8)
Zmodem
Forum Commoner
Posts: 84
Joined: Thu Apr 18, 2002 3:59 pm

Post by Zmodem »

you need a </option> closing tag for all your <option> tags :D 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>
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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)) &#123;
    // code if form has been posted
&#125; else &#123;
    // display form
&#125;
Mac
User avatar
Eugene
Forum Newbie
Posts: 24
Joined: Tue Jun 18, 2002 12:40 pm
Location: Montreal

REALLY ODD BEHAVIOUR FOR POST

Post by Eugene »

Mac

I changed your code to

Code: Select all

if (!isset($_POST&#1111;'music_key']))
&#123;
//POST not set, display form
&#125;
else
&#123;
//code if form was posted
&#125;
I did that because just

Code: Select all

isset($_POST)
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&#1111;'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
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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 :lol: .

Mac
User avatar
Eugene
Forum Newbie
Posts: 24
Joined: Tue Jun 18, 2002 12:40 pm
Location: Montreal

Post by Eugene »

I dont have one! :( I added it , but it doesn change anything. 8O

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
User avatar
Eugene
Forum Newbie
Posts: 24
Joined: Tue Jun 18, 2002 12:40 pm
Location: Montreal

Post 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
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post 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\" ...
DSM
Forum Contributor
Posts: 101
Joined: Thu May 02, 2002 11:51 am
Location: New Mexico, USA

Post 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.
User avatar
Eugene
Forum Newbie
Posts: 24
Joined: Tue Jun 18, 2002 12:40 pm
Location: Montreal

Post 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. 8)

And DSM, can I copy and use ur pic of Che????
Zmodem
Forum Commoner
Posts: 84
Joined: Thu Apr 18, 2002 3:59 pm

Post 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
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
Eugene
Forum Newbie
Posts: 24
Joined: Tue Jun 18, 2002 12:40 pm
Location: Montreal

Post by Eugene »

THANKS EVERYBODY :P

Works perfectly now :D

Eugene
DSM
Forum Contributor
Posts: 101
Joined: Thu May 02, 2002 11:51 am
Location: New Mexico, USA

Post by DSM »

thats great glad I can help.
Post Reply