homepage Welcome to WebmasterWorld Guest from 23.21.9.44
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
PHP form handler
Undefined variable?
cyclic




msg:1311759
 8:23 pm on Jun 28, 2002 (gmt 0)

I have scripted a simple enquiry form (HTML) to be handled by a PHP script. Every time I run it using IIS and PHP it comes back with "undefined variables" for all the fields. The form action is posting correctly to the PHP script.

Please point out the blindingly obvious that I am missing! I'm new to this....

 

mavherick




msg:1311760
 8:25 pm on Jun 28, 2002 (gmt 0)

If you could post the form code, I'm sure somebody here can help you out. Otherwise it's hard to tell. Could the method attribute be set to let's say post and you're trying to access your variables with $HTTP_GET_VARS array...

cyclic




msg:1311761
 8:31 pm on Jun 28, 2002 (gmt 0)

Here is a shortened version:

<FORM ACTION="HandleForm.php" METHOD=POST>
First Name <INPUT TYPE=TEXT NAME="FirstName" SIZE=20><BR>
Last Name <INPUT TYPE=TEXT NAME="LastName" SIZE=40><BR>
E-mail Address <INPUT TYPE=TEXT NAME="Email" SIZE=60><BR>
Comments <TEXTAREA NAME="Comments" ROWS=5 COLS=40></TEXTAREA><BR>
<INPUT TYPE=SUBMIT NAME="SUBMIT" VALUE="Submit!">
</FORM>

and the PHP script is:
<?php
print "Your first name is $FirstName.<br>\n";
print "Your last name is $LastName.<BR>\n";
print "Your E-mail address is $Email.<BR>\n";
print "This is what you had to say:<BR>\n $Comments<BR>\n";
?>

Could it be the way I have IIS set up? It handles stand alone PHP ok.

jatar_k




msg:1311762
 8:36 pm on Jun 28, 2002 (gmt 0)

try it this way

<?php
print "Your first name is $HTTP_POST_VARS["FirstName"].<br>\n";
print "Your last name is $HTTP_POST_VARS["LastName"].<BR>\n";
print "Your E-mail address is $HTTP_POST_VARS["Email"].<BR>\n";
print "This is what you had to say:<BR>\n $HTTP_POST_VARS["Comments"]<BR>\n";
?>

not sure but try it.

jatar_k




msg:1311763
 8:40 pm on Jun 28, 2002 (gmt 0)

or

foreach ($HTTP_POST_VARS as $key => $value)
{
echo "For " . $key . " you entered " . $value . "<BR>\n";
}

cyclic




msg:1311764
 8:49 pm on Jun 28, 2002 (gmt 0)

I now get the following:

Parse error: parse error, unexpected '\"', expecting T_STRING or T_VARIABLE or T_NUM_STRING

This is a bit beyond me at the moment

jatar_k




msg:1311765
 8:57 pm on Jun 28, 2002 (gmt 0)

I can't remember what that one means, did you use the method at post 4 or 5?

I am looking for that error.

mavherick




msg:1311766
 8:59 pm on Jun 28, 2002 (gmt 0)

you should be ok with the second option jatar_k offered but with the first there's a few quotes unescaped that could cause trouble.

<?php
print "Your first name is $HTTP_POST_VARS['FirstName'].<br>\n";
print "Your last name is $HTTP_POST_VARS['LastName'].<BR>\n";
print "Your E-mail address is $HTTP_POST_VARS['Email'].<BR>\n";
print "This is what you had to say:<BR>\n $HTTP_POST_VARS['Comments']<BR>\n";
?>

just change the double quotes to single quotes for all $HTTP_POST_VARS[''] variables.

jatar_k




msg:1311767
 9:00 pm on Jun 28, 2002 (gmt 0)

sorry cyclic, my fault.

cyclic




msg:1311768
 9:10 pm on Jun 28, 2002 (gmt 0)

Hey, no problem - I really appreciate all your help.

I have escaped the double quotes and now get:

Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING

This was meant to be a simple script to get me on my way! I don't yet understand Jatar_k's second option

mavherick




msg:1311769
 9:17 pm on Jun 28, 2002 (gmt 0)

i'm at work and can't test php right now but try this one.

<?php
print("Your first name is ".$HTTP_POST_VARS['FirstName'].".<br>\n");
print("Your last name is ".$HTTP_POST_VARS['LastName'].".<BR>\n");
print("Your E-mail address is ".$HTTP_POST_VARS['Email'].".<BR>\n");
print("This is what you had to say:<BR>\n". $HTTP_POST_VARS['Comments']."<BR>\n");
?>

cyclic




msg:1311770
 9:32 pm on Jun 28, 2002 (gmt 0)

Mav

That works - I just need to work out why! The first script I was using was from a manual about PHP - it all seemed to make sense until it wouldn't work. If you could explain your logic I would be grateful.

jatar_k




msg:1311771
 9:54 pm on Jun 28, 2002 (gmt 0)

this might be the valid reference.

"You can still force the old behaviour by setting register_globals [php.net] to 'On' in your php.ini file."

ergophobe




msg:1311772
 9:03 pm on Jul 1, 2002 (gmt 0)

This came up a few days ago

[webmasterworld.com...]

Tom

cyclic




msg:1311773
 9:21 pm on Jul 2, 2002 (gmt 0)

Thanks Tom

I have it sorted now!

mapgeek




msg:1311774
 7:19 pm on Aug 22, 2002 (gmt 0)

Hey there. I am also new to php and am having the same troubles as cyclic did. The book from which I am learning is assuming that global variables are on. I want to code with them off. I tried the fixes as suggested by mavherick and jatar, but am having no success. (I am running apache 2.0.40 on win2k.)
Here is my form:
<FORM METHOD=GET ACTION="check_time.php">

What is the best time to deliver?
<BR>
<BR> Please deliver from:
<Select Name="start">
<OPTION Value="9">09:00
<OPTION Value="10">10:00
<OPTION Value="11">11:00
<OPTION Value="12">12:00
<OPTION Value="13">13:00
<OPTION Value="14">14:00
<OPTION Value="15">15:00
<OPTION Value="16">16:00
</SELECT>
til
<SELECT NAME="end">
<OPTION Value="9">09:00
<OPTION Value="10">10:00
<OPTION Value="11">11:00
<OPTION Value="12">12:00
<OPTION Value="13">13:00
<OPTION Value="14">14:00
<OPTION Value="15">15:00
<OPTION Value="16">16:00
</SELECT>
<BR><BR>
<INPUT TYPE="Submit">
<INPUT TYPE="Reset">
</FORM>

and my code:

<?php
if ($HTTP_GET_VARS['end'] <= $HTTP_GET_VARS['start']) {
echo " We can only deliver between valid times.";
echo "<BR> We cannot deliver between";
echo "<BR><B>$HTTP_GET_VARS['end'] and $HTTP_GET_VARS['start']</B>";
echo "<a href=\"form_menu2.html\">Try Again</a>";
}
else
{
echo "Great! Expect a delivery between $HTTP_GET_VARS['start'] and $HTTP_GET_VARS['end']";
}
?>
my error is:
Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in ...\check_time.php on line 8
If I add the "." as Mavherick suggested, I get an unexpected "." error.
I tested the variables using jatar_k's foreach loop and it comes up correctly.
I'm so confused.

jatar_k




msg:1311775
 7:27 pm on Aug 22, 2002 (gmt 0)

Welcome to WebmasterWorld mapgeek

try changing this line

echo "<BR><B>$HTTP_GET_VARS['end'] and $HTTP_GET_VARS['start']</B>";

to

echo "<BR><B>" . $HTTP_GET_VARS['end'] . " and " . $HTTP_GET_VARS['start'] . "</B>";

That is my sneaking suspicion.

mapgeek




msg:1311776
 7:48 pm on Aug 22, 2002 (gmt 0)

Thank-you!
I finally got it to work.
Here's the code that did it:
<?php
# check_time.php

if ($HTTP_GET_VARS['end'] <= $HTTP_GET_VARS['start']) {
echo " We can only deliver between valid times.";
echo "<BR> We cannot deliver between";
echo "<BR><B>" . $HTTP_GET_VARS['start'] . " and " . $HTTP_GET_VARS['end'] . "</B>";
echo "<a href=\"form_menu2.html\"><BR>Try Again</a>";
}
else
{
echo "Great! Expect a delivery between " . $HTTP_GET_VARS['start'] . " and " .

$HTTP_GET_VARS['end'] ;
}
?>

Another question though... What is the significance of "." ?

jatar_k




msg:1311777
 8:02 pm on Aug 22, 2002 (gmt 0)

it is the concatenation operator.

It adds the two string values together.

andreasfriedrich




msg:1311778
 8:32 pm on Aug 22, 2002 (gmt 0)

As explained in [php.net...] when interpolating variables in double quoted strings the parser will greedily take as much tokens as possible to form a valid variable name. With array indices the closing ] marks the end of the index. Between the square brackets there must be an expression that evaluates to a string or integer.

Therefor when interpolation any $HTTP_*_VARS into a double quoted string use either one of the following methods:

- <? echo "ac rules: $HTTP_GET_VARS[ac]"; ?>
- <? echo "ac rules: {$HTTP_GET_VARS['ac']}"; ?>
- <? echo "ac rules: ${HTTP_GET_VARS['ac']}"; ?>
- completely avoid interpolation and use the . operator as has been suggested in previous posts.

Also note that the first way is correct only when used in a double quoted string as explained in [php.net...]

mapgeek




msg:1311779
 9:17 pm on Aug 22, 2002 (gmt 0)

Thanks again! You've been really helpful.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved