LinuxQuestions.org
Visit Jeremy's Blog.
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 06-19-2005, 11:29 AM   #1
blizunt7
Member
 
Registered: Mar 2004
Distribution: Fedora Core 1,2,3, RHEL3,4,5 Ubuntu
Posts: 274

Rep: Reputation: 30
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.
 
Old 06-19-2005, 11:46 AM   #2
comprookie2000
Gentoo Developer
 
Registered: Feb 2004
Location: Fort Lauderdale FL.
Distribution: Gentoo
Posts: 3,291
Blog Entries: 5

Rep: Reputation: 58
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.
 
Old 06-19-2005, 12:24 PM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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>
Code:


Last edited by Hko; 06-19-2005 at 12:26 PM.
 
Old 06-19-2005, 12:30 PM   #4
blizunt7
Member
 
Registered: Mar 2004
Distribution: Fedora Core 1,2,3, RHEL3,4,5 Ubuntu
Posts: 274

Original Poster
Rep: Reputation: 30
Great! thanks.

Where in this code, can i specify for example to send this data to /var/www/html/information ??
THANKS
 
Old 06-19-2005, 12:51 PM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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" >
 
Old 06-19-2005, 12:56 PM   #6
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
... 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.
 
Old 06-19-2005, 12:58 PM   #7
blizunt7
Member
 
Registered: Mar 2004
Distribution: Fedora Core 1,2,3, RHEL3,4,5 Ubuntu
Posts: 274

Original Poster
Rep: Reputation: 30
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??
 
Old 06-19-2005, 01:00 PM   #8
blizunt7
Member
 
Registered: Mar 2004
Distribution: Fedora Core 1,2,3, RHEL3,4,5 Ubuntu
Posts: 274

Original Poster
Rep: Reputation: 30
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
 
Old 06-19-2005, 01:30 PM   #9
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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.
 
Old 06-19-2005, 01:36 PM   #10
blizunt7
Member
 
Registered: Mar 2004
Distribution: Fedora Core 1,2,3, RHEL3,4,5 Ubuntu
Posts: 274

Original Poster
Rep: Reputation: 30
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
 
Old 06-19-2005, 01:38 PM   #11
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
You're welcome.
Good luck.
 
Old 06-19-2005, 05:34 PM   #12
comprookie2000
Gentoo Developer
 
Registered: Feb 2004
Location: Fort Lauderdale FL.
Distribution: Gentoo
Posts: 3,291
Blog Entries: 5

Rep: Reputation: 58
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.
 
Old 06-19-2005, 09:31 PM   #13
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,426

Rep: Reputation: 2786Reputation: 2786Reputation: 2786Reputation: 2786Reputation: 2786Reputation: 2786Reputation: 2786Reputation: 2786Reputation: 2786Reputation: 2786Reputation: 2786
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Html Form File Cottsay General 0 11-05-2005 02:30 PM
html question: form henrikanttonen Programming 4 08-13-2004 08:28 AM
How to capture video frames form webcam Madhukar Linux - Software 0 04-14-2004 08:26 AM
Contact form on HTML Gerardoj Linux - General 1 03-26-2004 03:08 AM
an html form question matt_w_lambert Programming 3 10-31-2003 12:41 AM

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

All times are GMT -5. The time now is 04:17 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