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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
06-19-2005, 11:29 AM
|
#1
|
Member
Registered: Mar 2004
Distribution: Fedora Core 1,2,3, RHEL3,4,5 Ubuntu
Posts: 274
Rep:
|
capture html form data
Hey all,
IM learning how to use forms in html, and PHP scripts. HTML is not a problem at all.
What I want to do is very simple, but i cant figure it out. Using forms, I want to have text boxes for user input. Such as first and last name.
I am using Apache 2.0.52 on Fedora Core 3.
I would like to be able to record the user input to a file on my server. Im not sure how to configure the form to do so??
ANyhelp, would be great!!
Thanks, Josh.
|
|
|
06-19-2005, 11:46 AM
|
#2
|
Gentoo Developer
Registered: Feb 2004
Location: Fort Lauderdale FL.
Distribution: Gentoo
Posts: 3,291
Rep:
|
You could look at how sphpblog does it.It is a nice little program that doesn't use a database.He has some nice scripts in there to give you some examples.
|
|
|
06-19-2005, 12:24 PM
|
#3
|
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536
Rep: 
|
Simple PHP-form example
Quote:
Originally posted by blizunt7
Using forms, I want to have text boxes for user input. Such as first and last name.
|
Have you made sure apache is running with mod_php enabled properly?
If it is, this simple PHP-form should run fine:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Simple form</title>
</head>
<body>
<?php
if (isset($_POST['submit'])) { // If submit-button is set, ...
// Process the form input:
echo 'Hi, <b>' . $_POST['firstname'] . ' ' . $_POST['lastname'] . '</b> <br/>';
echo 'How are you?';
} else { // Else, (submit-button is NOT set), ...
// Output the form. Line below ends php-processing,
// so after that we use plain html-code, ...
?>
<form name="simple" method="post" action="<?php echo $_REQUEST['URL']; ?>" >
<fieldset>
<br />
<legend> About you: </legend>
First name: <input type="text" name="firstname" />
<br />
<br />
Last name: <input type="text" name="lastname" />
<br />
<br />
</fieldset>
<br />
<input type="reset" value="Reset" />
<input type="submit" name="submit" value="OK" />
</form>
<?php } /* Short piece of php, just to end the if..else.. statement */ ?>
<!-- From here onward will always be send to browser,
because the php-if..else.. statement is finished
-->
</body>
</html>
Last edited by Hko; 06-19-2005 at 12:26 PM.
|
|
|
06-19-2005, 12:30 PM
|
#4
|
Member
Registered: Mar 2004
Distribution: Fedora Core 1,2,3, RHEL3,4,5 Ubuntu
Posts: 274
Original Poster
Rep:
|
Great! thanks.
Where in this code, can i specify for example to send this data to /var/www/html/information ??
THANKS
|
|
|
06-19-2005, 12:51 PM
|
#5
|
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536
Rep: 
|
Quote:
Originally posted by blizunt7
Where in this code, can i specify for example to send this data to /var/www/html/information ??
|
What exactly do you mean by "send this data to /var/www/html/information"?
Is "information" a PHP-script that should be run to process the form-data?
If so, in this line:
Code:
<form name="simple" method="post" action="<?php echo $_REQUEST['URL']; ?>" >
you can replace "<?php echo $_REQUEST['URL']; ?> by any PHP-filename on your server that contains code to process the form-data.
In the example I used <?php echo $_REQUEST['URL']; ?> to make PHP put the URL of the running PHP-script itself, no matter what name you would have saved the script, and no matter what directory you stored it in.
So, if your web-root directory is /var/www/html/, and you want the script information.php to be run when the user clicks the submit ("OK") button on the form, just use this instead:
Code:
<form name="simple" method="post" action="information.php" >
|
|
|
06-19-2005, 12:56 PM
|
#6
|
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536
Rep: 
|
... or, if you want to store the form-data in /var/www/html/information (i.e. write the data to a file) , please read the example on this page.
Last edited by Hko; 06-19-2005 at 01:00 PM.
|
|
|
06-19-2005, 12:58 PM
|
#7
|
Member
Registered: Mar 2004
Distribution: Fedora Core 1,2,3, RHEL3,4,5 Ubuntu
Posts: 274
Original Poster
Rep:
|
ahhhhh, i seeee.
So the URL of the PHP script, can use the variables in the PHP script on the page.
I am unfamiliar with PHP, so what i am basically looking for, is in the information.PHP script, i can write to a file, the first and last name, each time the submit button is pressed??
ANd this is something else, than what you have provided for me??
|
|
|
06-19-2005, 01:00 PM
|
#8
|
Member
Registered: Mar 2004
Distribution: Fedora Core 1,2,3, RHEL3,4,5 Ubuntu
Posts: 274
Original Poster
Rep:
|
OHHHH PERFECT!! thanks so much HKO.
So this code, that opens the file, and writes to it, this is contained in information.PHP?? and i still keep the form data you wrote above the same??????
this is great! thanks again
|
|
|
06-19-2005, 01:30 PM
|
#9
|
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536
Rep: 
|
Quote:
Originally posted by blizunt7
ANd this is something else, than what you have provided for me??
|
So, you didn't try what it does...
OK. Last example (from me at least).
Try it, and figure out what it does. If you don't get it, please read up on PHP.
Create 2 files in /var/www/html: - File: "entername.html" (plain html):
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Submit-your-name-form</title>
</head>
<body>
<h1>Submit-your-name-form</h1>
<form name="simple" method="post" action="storename.php" >
<fieldset>
<legend> About you: </legend>
<br />
First name: <input type="text" name="firstname" />
<br />
<br />
Last name: <input type="text" name="lastname" />
<br />
<br />
</fieldset>
<br />
<input type="reset" value="Reset" />
<input type="submit" name="submit" value="OK" />
</form>
</body>
</html>
- File: "storename.php" (html + php-code):
PHP Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> Storing your name </title>
</head>
<body>
<h1> Storing your name </h1>
<?php
$filename = "/var/tmp/information.txt";
$info = 'First name: "' . $_POST['firstname'] .
'", Last name: "' . $_POST['lastname'] .
"\"\n";
// Code below taken and (changed a little) from the
// example at:
// http://www.php.net/manual/en/function.fwrite.php
//
// Open the file for appending:
if (!$handle = @fopen($filename, 'a')) {
echo "Cannot open file ($filename) for writing";
echo "</body> </html>\n";
exit;
}
// Write to the file handle:
if (@fwrite($handle, $info) === FALSE) {
echo "Cannot write to file ($filename)";
echo "</body> </html>\n";
exit;
}
// If we get here, everything went fine.
echo "Your name was written to a file <br />";
fclose($handle);
?>
</body>
</html>
This will write the form-data to the file /var/tmp/information.txt.
Note: not to "/var/www/...", because the web-server (apache) cannot write to this directory. But any user (on you system) can write to /var/tmp.
Try enter some names in the form a few times. Then check out the contents of /var/tmp/information.txt.
Last edited by Hko; 06-19-2005 at 01:41 PM.
|
|
|
06-19-2005, 01:36 PM
|
#10
|
Member
Registered: Mar 2004
Distribution: Fedora Core 1,2,3, RHEL3,4,5 Ubuntu
Posts: 274
Original Poster
Rep:
|
THANK YOU SO MUCH.
YEa sorry i did not test it, i was just looking at the code, trying to understand it. THis helps greatly! thanks so much for everything
JOsh
|
|
|
06-19-2005, 01:38 PM
|
#11
|
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536
Rep: 
|
You're welcome.
Good luck.
|
|
|
06-19-2005, 05:34 PM
|
#12
|
Gentoo Developer
Registered: Feb 2004
Location: Fort Lauderdale FL.
Distribution: Gentoo
Posts: 3,291
Rep:
|
Hko , thanks for taking the time to do this.It really helps to understand how it all works with an example,I got alot out of your post.
|
|
|
06-19-2005, 09:31 PM
|
#13
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,426
|
If you haven't already done so, I suggest you bookmark this: http://www.php.net/manual/en/
As well as being the manual for PHP, each definition also contains 1 (usually more) examples of usage 
I've found it invaluable.
|
|
|
All times are GMT -5. The time now is 04:17 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|