Welcome to WebmasterWorld Guest from 54.147.189.54

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

PHP form handler

Undefined variable?

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

Junior Member

10+ Year Member

joined:June 13, 2002
posts:108
votes: 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....

8:25 pm on June 28, 2002 (gmt 0)

Junior Member

10+ Year Member

joined:June 3, 2002
posts:169
votes: 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...
8:31 pm on June 28, 2002 (gmt 0)

Junior Member

10+ Year Member

joined:June 13, 2002
posts:108
votes: 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.

8:36 pm on June 28, 2002 (gmt 0)

Administrator

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 24, 2001
posts:15755
votes: 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.

8:40 pm on June 28, 2002 (gmt 0)

Administrator

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 24, 2001
posts:15755
votes: 0


or

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

8:49 pm on June 28, 2002 (gmt 0)

Junior Member

10+ Year Member

joined:June 13, 2002
posts:108
votes: 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

8:57 pm on June 28, 2002 (gmt 0)

Administrator

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 24, 2001
posts:15755
votes: 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.

8:59 pm on June 28, 2002 (gmt 0)

Junior Member

10+ Year Member

joined:June 3, 2002
posts:169
votes: 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.

9:00 pm on June 28, 2002 (gmt 0)

Administrator

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 24, 2001
posts:15755
votes: 0


sorry cyclic, my fault.
9:10 pm on June 28, 2002 (gmt 0)

Junior Member

10+ Year Member

joined:June 13, 2002
posts:108
votes: 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

9:17 pm on June 28, 2002 (gmt 0)

Junior Member

10+ Year Member

joined:June 3, 2002
posts:169
votes: 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");
?>

9:32 pm on June 28, 2002 (gmt 0)

Junior Member

10+ Year Member

joined:June 13, 2002
posts:108
votes: 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.

9:54 pm on June 28, 2002 (gmt 0)

Administrator

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 24, 2001
posts:15755
votes: 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."

9:03 pm on July 1, 2002 (gmt 0)

Moderator

WebmasterWorld Administrator ergophobe is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Apr 25, 2002
posts:8135
votes: 93


This came up a few days ago

[webmasterworld.com...]

Tom

9:21 pm on July 2, 2002 (gmt 0)

Junior Member

10+ Year Member

joined:June 13, 2002
posts:108
votes: 0


Thanks Tom

I have it sorted now!

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

New User

10+ Year Member

joined:Aug 22, 2002
posts:10
votes: 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.

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

Administrator

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 24, 2001
posts:15755
votes: 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.

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

New User

10+ Year Member

joined:Aug 22, 2002
posts:10
votes: 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 "." ?

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

Administrator

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 24, 2001
posts:15755
votes: 0


it is the concatenation operator.

It adds the two string values together.

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

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:July 22, 2002
posts:1782
votes: 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...]

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

New User

10+ Year Member

joined:Aug 22, 2002
posts:10
votes: 0


Thanks again! You've been really helpful.