Click to See Complete Forum and Search --> : PHP help


nawkternalwonde
12-10-2003, 01:50 PM
I am new to PHP, and I am having trouble with this simple scritp. Please let me know what I am doing wrong.

HTML form:


<html>

<head>

<title>Practice Page</title>


<style type="text/css">

p {color: white;}
body {background-color: purple; font-family: times;}
p.big {font size: 200%; color: #11f111; margin-left: 6cm; margin-right: 4cm}
p.margin {margin-left: 4cm; margin-right: 6cm;}

</style>

</head>

<body>

<p class="big">My first form<sup>2</sup></p>

<form action="handleform.php" method=post>

First Name <br /> <INPUT TYPE=TEXT NAME="FirstName" SIZE=20>

<br />

Last Name <br /><input type=text name="LastName" size=40>

<br />

Email Address <br /><input type="text" name="Email" size="60"

<br />
<br />

Comments <br /><textarea name="Comments" rows="5" cols="40"></textarea>

<br />

<input type="submit" name="submit" value="submit!">

</form>

<br />
<br />

</body>
</html>

PHP script:

<html>

<head>

<title>handelform.php</title>

</head>

<body>


<?php
/* this form handles info from form.htm */

print "Your first name is $FirstName.<br />\n";
print "Your last name is $LastName.<br />\n";
print "Your email is $Email.<br />\n";
print "This is what you had to say:<br />\n
$Comments<br />\n";

?>


</body>
</html>

ph34r
12-10-2003, 01:57 PM
You need to import the form variables and values. Look at the php.net site for the manual, and import_request_variables in the functions.

nawkternalwonde
12-10-2003, 02:14 PM
I thank you for the link, but I have already been there, and it doesn't explain very throughly, and there are no examples. Maybe there is another person that can just see what I am doing wrong. Thank you.

ph34r
12-10-2003, 02:23 PM
This should work.... notice the import request variables.... using both get and post, but post takes precedent. Each variable from the form will have rvar_ appended to the name, so your email field becomes $rvar_email.


<html>
<head>
<title>handelform.php</title>
</head>
<body>
<?php
/* this form handles info from form.htm */
import_request_variables("gP", "rvar_");
print "Your first name is $rvar_FirstName.<br />\n";
print "Your last name is $rvar_LastName.<br />\n";
print "Your email is $rvar_Email.<br />\n";
print "This is what you had to say:<br />\n
$rvar_Comments<br />\n";
?>
</body>
</html>

nawkternalwonde
12-10-2003, 03:00 PM
I am pretty upset, I copied that script from a book that I bouhgt to teach myself PHP. If that book bad scripts that are that simple, what does that say about the rest of the book? argggg.

Thank you.

Choozo
12-10-2003, 04:48 PM
Depending on how old that book is/was, the examples may quite well be correct! There was a significant change in PHP > 4.2 where POST variables had to be handled with great care - if you didn't care to set 'register_globals=off' in your php.ini file.

Using $_POST["variablename"] should also fix your problems.
E.g.: $_POST["FirstName"]

Cheers :)

sploo22
12-10-2003, 05:35 PM
You can also use $_REQUEST["FirstName"], which works for either GET or POST requests. That way you don't have to change your script if you change the form method.

nawkternalwonde
12-11-2003, 02:00 PM
Now I am getting "Parse error: parse error, unexpected '"', expecting T_STRING or T_VARIABLE or T_NUM_STRING in D:\HTML\handleform.php on line 16"


Frustration.

mXskweeb
12-11-2003, 02:59 PM
A lot of PHP resources don't distinguish between:echo "concatenate $me and me";
and
echo "conancatenate".$me."and me";
My understanding is that PHP automagically evaluates variables when they are encountered inside a quoted string so you don't have to close the string and use the concatenation operator (the dot in PHP) to mix variables with string expressions. I recall seeing some explanation as to how when and why PHP does this, but the way I figure, explicitly setting off concatenations as in the second example is always correct if that is your intent. Plus, I figured that behavior can probably be changed in php.ini since everything else can be, or might react differently under different environments, so I just always do it the long way. I may be being paranoid, but I didn't really bother trying to grok the explanation of how it worked since I figured I was already in the habit from other languages.

Anyway, I don't recognize your specific error msg, but I would guess since it's complaining about closing the quoted string and yours include the vars inside the quotes, that might help. Maybe one of the PHP gurus can jump in and clarify my jumbled explanation above. Or actually one would probably find reader comments on it in the on-line PHP manual under the relative section.

Edit
I did some reading in the manual, and I couldn't find anything to support my assertion above, so apparently I'm full of crap. Alwell, it's a habit now.

Choozo
12-11-2003, 03:53 PM
Originally posted by nawkternalwonde
Now I am getting "Parse error: parse error, unexpected '"', expecting T_STRING or T_VARIABLE or T_NUM_STRING in D:\HTML\handleform.php on line 16"


Frustration. Typically a missing ';' (semicolon) in the line above the reported line.

nawkternalwonde
12-11-2003, 05:14 PM
Choozo was right. If you look at line 12 in the PHP code, the comment that I entered. I didn't add the comma like I should have. The code is working fine now. Thank you guys so much for you time. A special thanks to Choozo, for not overlooking the small stuff.

Choozo
12-11-2003, 05:48 PM
Originally posted by nawkternalwonde
... for not overlooking the small stuff. Usually the "small stuff" is what causes hard-to-spot problems, and are thus not so small in the end :D

Cheers :)

Trogdor
12-16-2003, 12:27 AM
Originally posted by sploo22
You can also use $_REQUEST["FirstName"], which works for either GET or POST requests. That way you don't have to change your script if you change the form method.

That's what I always use.