LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-04-2007, 06:42 AM   #1
noir911
Member
 
Registered: Apr 2004
Posts: 682

Rep: Reputation: Disabled
PHP form


I am trying to write a HTML form which will save data in a PHP form for later view. But looks like it's posting (POST) the data to the PHP form and it's not showing up.

Here's my HTML form

Code:
<HTML> <HEAD> <TITLE> HTML FORM </TITLE> </HEAD> <Body> 
<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" ROW=5 COLS=40> </TEXTAREA><BR>

<INPUT TYPE=SUBMIT NAME="SUBMIT" VALUE="Submit!">

</FORM>

</body> </html>
Here's my PHP form

Code:
<HTML><HEAD><TITLE>Form Results </TITLE><BODY>
<?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";
?>
</BODY>
</HTML>
Here's the output I get

Code:
Your first name is .
Your last name is .
Your E-mail address is .
This is what you had to say:

Some help would be appreciated.

Thanks.

Last edited by noir911; 04-04-2007 at 06:48 AM.
 
Old 04-04-2007, 07:15 AM   #2
Centinul
Member
 
Registered: Jun 2005
Distribution: Gentoo
Posts: 552

Rep: Reputation: 30
Try:

Code:
<HTML><HEAD><TITLE>Form Results </TITLE><BODY>
<?php
print "Your first name is $_POST["FirstName"].<BR>\n";
print "Your last name is $_POST["LastName"].<BR>\n";
print "Your E-mail address is $_POST["Email"].<BR>\n";
print "This is what you had to say:<BR>\n $_POST["Comments"]<BR>\n";
?>
</BODY>
</HTML>
Since you are using the POST method you have to reference the POST array.

Google for PHP and POST and you should see some tutorials.

Hope this helps,

Centinul
 
Old 04-04-2007, 07:39 AM   #3
Spudley
Member
 
Registered: Mar 2003
Location: Berkshire, England.
Distribution: SuSE 10.0
Posts: 299

Rep: Reputation: 32
Yes, as previous reply says, the results of your post are in the $_POST[] array.

If you use a get query, the results are in $_GET[].

There is also an array called $_REQUEST[] which includes the contents of both of these (and also any cookies you may have). You can use this instead of $_POST or $_GET if you're not sure which method will be used, but if you are sure, it's better to use $_POST or $_GET.

The reason you're not getting results with your existing code is that you are using a style of coding that is out of date. PHP version 3 created individual variables for each form field, so your code would work fine in PHP 3. However, this was found to be a serious security risk, so PHP 4 and later started using the $_POST[], etc arrays instead.

It is possible to turn the old functionality back on again if you really need it (ie if you have an old program that uses it); you need to look up "register globals" in the PHP documentation. However, it is better to stick with the new style wherever possible.

PHP4 has been around for a long time now, so this isn't new; I guess you've been reading some badly out-dated tutorials if you're writing code like this?

Since I'm on the subject of out-dated code, you should also look into the more up-to-date style of writing HTML. Google for XHTML for more info. Your HTML there will work fine, but it's always good to use the most modern techniques.

Hope that helps. All the best.
 
Old 04-04-2007, 08:09 AM   #4
Spudley
Member
 
Registered: Mar 2003
Location: Berkshire, England.
Distribution: SuSE 10.0
Posts: 299

Rep: Reputation: 32
Additional: I should point out that the first reply is also incorrect PHP. The array fields can't be embedded in print statements like that. Either of the following will work:
PHP Code:
print "Your first name is {$_POST["FirstName"]}.<br />\n";
print 
"Your first name is ".$_POST["FirstName"].".<br />\n"
(note, I've changed the <BR>s into <br /> for the newer HTML style I mentioned)
 
Old 04-04-2007, 08:47 AM   #5
Centinul
Member
 
Registered: Jun 2005
Distribution: Gentoo
Posts: 552

Rep: Reputation: 30
Spudley --

Thanks for the correction. I don't code PHP much
 
Old 04-04-2007, 04:38 PM   #6
noir911
Member
 
Registered: Apr 2004
Posts: 682

Original Poster
Rep: Reputation: Disabled
I tried what Centinul and Spudley suggested and now I can see the output.

BUT the PHP form doesn't _save_ the data for later viewing. How can I save the data in the PHP so that someone can view it later?

Here's the current PHP code

Code:
<HTML><HEAD><TITLE>Form Results </TITLE></HEAD><BODY>
<?php
print "Your first name is ".$_POST["FirstName"].".<BR>\n";
print "Your last name is ".$_POST["LastName"].".<BR>\n";
print "Your E-mail address is ".$_POST["Email"].".<BR>\n";
print "This is what you had to say:<BR>\n ".$_POST["Comments"]."<BR>\n";
?>
</BODY>
</HTML>
Here's the HTML code

Code:
<HTML> <HEAD> <TITLE> HTML FORM </TITLE> </HEAD> <Body>
<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" ROW=5 COLS=40> </TEXTAREA><BR>
<INPUT TYPE=SUBMIT NAME="SUBMIT" VALUE="Submit!">
</FORM>
</body> </html>
I know saving data maybe insecure but I need to do it. Is there any way to do it without talking to a database? If not, any simple example on how to do it through a database?

Thanks for all the help.

Last edited by noir911; 04-04-2007 at 04:46 PM.
 
Old 04-04-2007, 05:34 PM   #7
Spudley
Member
 
Registered: Mar 2003
Location: Berkshire, England.
Distribution: SuSE 10.0
Posts: 299

Rep: Reputation: 32
Cool

Quote:
Originally Posted by noir911
I tried what Centinul suggested & error_log was giving error:

PHP Parse error: syntax error, unexpected '"', expecting T_STRING or T_VARIABLE or T_NUM_STRING
That's because of what I said in my reply earlier about having array variables embedded in a string.

Quote:
So I removed " from the code, now it displays the data but I still get error in error_log:
Quote:
PHP Notice: Undefined index: FirstName in /path/to/php/file
If it is displaying the data, then the index is defined, so I don't know what the log message is telling you. It would show if you called the page without posting the data, but you didn't say you'd done that.

But in any case, it's not something to worry about: it's only a 'notice', so you can safely ignore it: PHP knows how to handle it if you ask for an undefined index; it's just warning you here in case you weren't expecting it.

Removing the quotes from array fields will work (as you discovered), but is not the preferred way of doing it; as with your original error, it is an out-dated method of doing things in PHP. See my previous post for the correct syntax, using {curly braces} to embed an array variable in a string.

As you see, the way you've done it works, but it is

Quote:
The PHP form doesn't _save_ the data for later viewing. How can I save the data in the PHP form so that someone can view it later?
Ah, now that's a whole other tutorial. You'll need to google for the PHP file handling functions (fread(), fwrite(), gets(), puts(), etc) and possibly the database handling functions, if you have access to one (probably mysql_xxx, though there are other databases). Far too much in there to discuss the whole topic here, but google for some tutorials.

Quote:
Here's the current PHP code (...snip...)
Please please please make your HTML code XHTML-compliant. It's making my eyes bleed looking at that code!!!

1) Lower case tags.
2) All attribute values enclosed in quotes.
3) All tags closed, even <br /> and <input ... />

Okay, so you don't *have* to do that if you don't want to; rather spend time making your code work first! ... it's just that I've spent a lot of time and effort fixing other people's HTML recently. The XHTML standard has been around for some years now, and it's a bit heart-breaking to see the old coding styles still being written.

Out of interest, are you just learning PHP now? If so, where are you learning it from? You really need to get some more up-to-date tutorials -- all the mistakes you've made are correct coding style for PHP3, but that's been obsolete for years.
 
Old 04-06-2007, 05:55 AM   #8
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
How long does it need to be accessible. After a user has closed the browser and reconnects again or only during the session?
For the former, see above. For the latter, have a look at sessions (or possibly cookies).
 
  


Reply



Posting 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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP form noir911 Programming 3 02-12-2007 10:04 PM
No form output in PHP jabfinger Programming 2 07-15-2005 06:48 PM
PHP form help Zeppelin_Fan Programming 5 03-24-2005 04:47 PM
PHP to sendmail from form lawadm1 Linux - Software 1 08-16-2004 11:55 PM
php/form kev82 Programming 0 02-25-2004 06:21 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:16 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration