LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   capture html form data (http://www.linuxquestions.org/questions/programming-9/capture-html-form-data-335148/)

blizunt7 06-19-2005 11:29 AM

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.

comprookie2000 06-19-2005 11:46 AM

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.

Hko 06-19-2005 12:24 PM

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:



blizunt7 06-19-2005 12:30 PM

Great! thanks.

Where in this code, can i specify for example to send this data to /var/www/html/information ??
THANKS

Hko 06-19-2005 12:51 PM

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" >

Hko 06-19-2005 12:56 PM

... 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.

blizunt7 06-19-2005 12:58 PM

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??

blizunt7 06-19-2005 01:00 PM

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

Hko 06-19-2005 01:30 PM

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.

blizunt7 06-19-2005 01:36 PM

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

Hko 06-19-2005 01:38 PM

You're welcome.
Good luck.

comprookie2000 06-19-2005 05:34 PM

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.

chrism01 06-19-2005 09:31 PM

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 10:17 PM.