Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > learning php, problem w/ forms

learning php, problem w/ forms
Thread Tools
Junior Member
Join Date: Jul 2001
Status: Offline
Reply With Quote
Jul 28, 2002, 11:26 PM
 
hi, i bought a book on learning php from B&N recently, the php package i have installed on my machine is the one from Marc Liyanage's site. in the beginning of the book, he shows how to set up a simple form that lets you fill out info and then it is spit back at you on the next page. however, it does not work for me, even tho i copied the source, charachter for charachter from the book. the form i have set up is @ <a href="http://thunderpoit.shacknet.nu/php_lernin/form.html" target="_blank">http://thunderpoit.shacknet.nu/php_lernin/form.html</a> . btw, the book is PHP for the world wide web by larry ullman and is from the visual quickstart series. here is the source for the form and the form handler:

html form:
html&gt;
head&gt;
title&gt;HTML Form/title&gt;
/head&gt;
body&gt;
form action="handleform.php" method=post&gt;
First Name input type=text name="FirstName" size=20&gt;br&gt;
Last Name input type=text name="LastName" size=40&gt;br&gt;
E-mail Address input type=text name="Email"size=60&gt;br&gt;
Comments textarea name="Comments" rows=5 cols=40&gt;/textarea&gt;br&gt;
input type=submit name="submit" value="Submit!"&gt;
/form&gt;
/body&gt;
/html&gt;

form handlder:
html&gt;
head&gt;
title&gt;Form Results/title&gt;
body&gt;
?php
print "your first name is $FirstName.br&gt;\n";
print "your last name is $LastName.br&gt;\n";
print "your E-mail address is $Email.br&gt;\n";
print "this is what you had to say: br&gt;\n $Commentsbr&gt;\n";
?&gt;
/body&gt;
/html&gt;

what it spits out is:

your first name is .
your last name is .
your E-mail address is .
this is what you had to say:

anyone know why it wont work? do i have to get the full version of php from php.net or should the version that i have work fine?

thx in advance,
-php newb.

<small>[ 07-29-2002, 12:29 AM: Message edited by: ThunderP ]</small>
     
Senior User
Join Date: Feb 2001
Location: Chattanooga, TN
Status: Offline
Reply With Quote
Jul 28, 2002, 11:45 PM
 
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">Originally posted by ThunderP:
<strong>
code, code, code</strong></font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">All of your tags are missing the leading "&lt;". I'm assuming that you edited them out because you can't post html on the forums. If you didn;t edit them out and they are truly missing, try adding them in One other thing to check is to make sure when you process the forum using php that you are calling the same variable that the form defines. If you want the results stored in $name, call $name...not $name.br&gt; or anything.
Yeah, about those TPS reports, didn't you get the memo?
--------------
2.3 Ghz Dual-Core G5/20" LCD/Life is Good!
Moo...
     
Forum Regular
Join Date: Apr 2001
Location: NY, NY, USA
Status: Offline
Reply With Quote
Jul 28, 2002, 11:52 PM
 
What version of PHP are you using? If 4.2 or above you should:
echo "your first name is $HTTP_POST_VARS[FirstName].br&gt;\n";

Read <a href="http://www.php.net/release_4_2_0.php" target="_blank">http://www.php.net/release_4_2_0.php</a> for more info about external variables (i.e. using $HTTP_POST_VARS[FirstName] or $_POST[FirstName] instead of simply $FirstName). You may or may not want to turn on register_globals in your php.ini file so you can use $FirstName but be sure to read that page first.
- Sahara
     
Mac Elite
Join Date: Feb 2001
Location: Bristol, UK, living in Melbourne, Australia
Status: Offline
Reply With Quote
Jul 28, 2002, 11:57 PM
 
edit what version of php are you running - there was a change recently in the defaults that would break your script, namely register_globals was ON and is now OFF. Thus your form variables are currently only accessible using the $_POST variable (an array).

Changing your php to read as follows should fix it:

</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">&lt;?php
print &quot;your first name is $_POST[&quot;FirstName&quot;].&lt;br&gt;\n&quot;;
print &quot;your last name is $_POST[&quot;LastName&quot;].&lt;br&gt;\n&quot;;
print &quot;your E-mail address is $_POST[&quot;Email&quot;].&lt;br&gt;\n&quot;;
print &quot;this is what you had to say: &lt;br&gt;\n $_POST[&quot;Comments&quot;]&lt;br&gt;\n&quot;;
?&gt;</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">However you could also create a php.ini file and include in it the line:

register_globals = On

To find out where the php.ini file should be, create a php file called phpinfo.php containing the line </font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">&lt;?php phpinfo(); ?&gt;</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">and then open it in your browser. Amongst all the other useful information is the location of your php.ini file. You may have to create it if it doesn't already exist in that location. (/usr/local/lib/php.ini on my computer).

Hope this helps
     
Mac Elite
Join Date: Feb 2001
Location: Bristol, UK, living in Melbourne, Australia
Status: Offline
Reply With Quote
Jul 29, 2002, 12:00 AM
 
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">Originally posted by sahara:
<strong>What version of PHP are you using? If 4.2 or above you should:
echo "your first name is $HTTP_POST_VARS[FirstName].br&gt;\n";

Read <a href="http://www.php.net/release_4_2_0.php" target="_blank">http://www.php.net/release_4_2_0.php</a> for more info about external variables (i.e. using $HTTP_POST_VARS[FirstName] or $_POST[FirstName] instead of simply $FirstName). You may or may not want to turn on register_globals in your php.ini file so you can use $FirstName but be sure to read that page first.</strong></font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">beeyatch! I though I had got there first

As for the register_globals directive, it doesn't really make php any more secure at all IMO and it is pretty harmless to leave it on. However it is sensible to stick to using $_POST on any scripts you write from now on as they will run whether the directive is set or not. I believe $HTTP_POST_VARS is for pre version 4.1 but I may be wrong...
     
Forum Regular
Join Date: Apr 2001
Location: NY, NY, USA
Status: Offline
Reply With Quote
Jul 29, 2002, 08:34 AM
 
Yeah $HTTP_POST_VARS has been replaced with $_POST but I wasn't positive what version he was using, and I often still use $HTTP_POST_VARS for backwards compatibility. I write a lot of PHP software that has to work on different versions. $HTTP_POST_VARS still works on 4.2.x even though $_POST has been added.

As for security, I don't often run into situations where register_globals on is a problem, but it can be. Especially in multi-user environments where security is a must. At any rate, if you're a careful coder you can make a secure system even with register_globals turned on. You just have to be aware of what can be done when it's on.
- Sahara
     
Junior Member
Join Date: Jul 2001
Status: Offline
Reply With Quote
Jul 29, 2002, 02:23 PM
 
hey, wow, theres a little code button that lets u post code, amazing how observant i am.......
the version of php i am running is 4.2.1. so enough has changed in php that the book im using is obsolete?

i tried using the edited code that tinrib posted:
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;"> &lt;?php
print &quot;your first name is $_POST[&quot;FirstName&quot;].&lt;br&gt;\n&quot;;
print &quot;your last name is $_POST[&quot;LastName&quot;].&lt;br&gt;\n&quot;;
print &quot;your E-mail address is $_POST[&quot;Email&quot;].&lt;br&gt;\n&quot;;
print &quot;this is what you had to say: &lt;br&gt;\n $_POST[&quot;Comments&quot;]&lt;br&gt;\n&quot;;
?&gt;</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">however, i got htis as an error:
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">

Parse error: parse error, unexpected '\"', expecting T_STRING or T_VARIABLE or T_NUM_STRING in /Library/WebServer/Documents/php_lernin/handleform.php on line 7
</font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">
     
Forum Regular
Join Date: Apr 2001
Location: NY, NY, USA
Status: Offline
Reply With Quote
Jul 29, 2002, 03:22 PM
 
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">Originally posted by ThunderP:
<strong>hey, wow, theres a little code button that lets u post code, amazing how observant i am.......
the version of php i am running is 4.2.1. so enough has changed in php that the book im using is obsolete?

i tried using the edited code that tinrib posted:
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;"> &lt;?php
print &quot;your first name is $_POST[&quot;FirstName&quot;].&lt;br&gt;\n&quot;;
print &quot;your last name is $_POST[&quot;LastName&quot;].&lt;br&gt;\n&quot;;
print &quot;your E-mail address is $_POST[&quot;Email&quot;].&lt;br&gt;\n&quot;;
print &quot;this is what you had to say: &lt;br&gt;\n $_POST[&quot;Comments&quot;]&lt;br&gt;\n&quot;;
?&gt;</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">however, i got htis as an error:
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">

Parse error: parse error, unexpected '\"', expecting T_STRING or T_VARIABLE or T_NUM_STRING in /Library/WebServer/Documents/php_lernin/handleform.php on line 7
</font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif"></strong></font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">Whatever book you are using is probably fine. But there are some things that have changed from release to release. If you scan the release announcements you can see what has changed between the version your book is using and the version you are. PHP has not changed dramatically enough for me to believe your book is unusable.
As for your error, the problem is with your quotes. For example:
print "your first name is $_POST["FirstName"].&lt;br&gt;\n";
is incorrect because you cannot put quotes inside quotes. You could either:
1) print "your first name is $_POST[\"FirstName\"].&lt;br&gt;\n";
or
2) print "your first name is $_POST[FirstName].&lt;br&gt;\n";
In 1, the backslash in front of the quotes around FirstName "escapes" the quotes so you can embed them inside other quotes. In 2, you can see that the quotes are actually not required to access a $_POST element. Another way you can deal with this is:
print "your first name is ".$_POST["FirstName"].".&lt;br&gt;\n";
Your book should cover these syntax issues. Good luck!

<small>[ 07-29-2002, 04:24 PM: Message edited by: sahara ]</small>
- Sahara
     
Junior Member
Join Date: Jul 2001
Status: Offline
Reply With Quote
Jul 29, 2002, 03:27 PM
 
ahhh, thankies
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 11:15 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2