PHP forms

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bengaluru
Forum Newbie
Posts: 6
Joined: Sun Dec 28, 2003 9:53 am
Location: Toronto

PHP forms

Post by bengaluru »

Hi Guys,

I am new to this forum and to PHP. I have absolutely no idea how it works and thought would get some help.

I was developing a personal website and wanted to get feedback from my visitors in a HTML form.

This is the simplest form that I am trying as a beginner. I am using the method as Post in my HTML code and wanted to know how I create a PHP file so that the information my user inputs gets stored. For example if I have the code as below:

Code: Select all

<form action="formresponse.php" method="post">
              <table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
                <tr> 
                  <td colspan="2"> 
                    <div align="center"></div>
                  </td>
                </tr>
                <tr> 
                  <td>Your Full Name:</td>
                  <td> 
                    <input type="text" name=" name" size="40">
                  </td>
                </tr>
                <tr> 
                  <td>Your Email:</td>
                  <td> 
                    <input type="text" name="email" size="40">
                  </td>
                </tr>
                <tr> 
                  <td>Where are you from:</td>
                  <td> 
                    <input type="text" name="Place" size="40">
                  </td>
                </tr>
                <tr> 
                  <td>Your Website:</td>
                  <td> 
                    <input name="website" type="text" id="website" value="http://" size="40">
                  </td>
                </tr>
                <tr> 
                  <td>Your Comments:</td>
                  <td> 
                    <textarea name="comments" cols="35" rows="4" id="comments"></textarea>
                  </td>
                </tr>
                <tr> 
                  <td>&nbsp;</td>
                  <td><br>
                    <input type="submit" name="Submit" value="Submit">
                  </td>
                </tr>
              </table>
            </form>
[mod_edit:

Code: Select all

-tag added][/size]

How do I create my formresponse.php file ?

My server operating system is Linux which uses PHP 4.3.3.  

Hope I have made myself clear as to what I am looking for.

Thanks for your help.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

there's a tutorial at php.net covering this topic
http://www.php.net/manual/en/tutorial.forms.php
bengaluru
Forum Newbie
Posts: 6
Joined: Sun Dec 28, 2003 9:53 am
Location: Toronto

Post by bengaluru »

Thanks.

I have already gone through all the tutorials but that does not answer my question.

I would appreciate if a link that provides answer to the speciifc php file is posted.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

that provides answer to the speciifc php file is posted
:?:

Your form has the action-property "post", so in your php-script the values will be stored in the superglobal array $_POST.
E.g.
<input type="text" name="email" size="40"> :arrow: $_POST['email']

Where and how do you want to store the data?
bengaluru
Forum Newbie
Posts: 6
Joined: Sun Dec 28, 2003 9:53 am
Location: Toronto

Post by bengaluru »

Thanks Volka,

You got me right. In fact that was my question. How and where does the data entered by the user gets stored ?? I believe that it gets stored in the formresponse.php that i would have to upload to my root folder in my server.

My question was How do I create the formresponse.php file.
bengaluru
Forum Newbie
Posts: 6
Joined: Sun Dec 28, 2003 9:53 am
Location: Toronto

Post by bengaluru »

Further to my question, if I make the form action "mail to : abc@xyz.com, Will the response get emailed to the E mail that I specify ??
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post by DuFF »

The formresponse.php file is just like any other PHP file. At the top of the page you can use the posted variables (like $_POST['email'] or $_POST['name'] ) and do whatever you want with them.

An example would be like this:

Code: Select all

<?php
if (empty($_POST['name']))
{
    echo "You didn't enter a name!";
}
else
{
    //keep going
    //You can put the info into a database after you have checked the validity of it
    $query = "INSERT INTO table VALUES ('$POST['name']','$POST['email']','$POST['Place']','$POST['website']','$POST['comments']')";
    $result = mysql_query($query);
}

?>
Edit:
Just saw your post regarding the email. I'm not sure about that but instead of inserting the data into a database as shown above you could just email it to your self using PHP. Check out the mail() function.
bengaluru
Forum Newbie
Posts: 6
Joined: Sun Dec 28, 2003 9:53 am
Location: Toronto

Post by bengaluru »

Thanks very much duFF.

Although I haven't understood it fully, I wil try it out. when I tried to test it by entering data and clicking on the submit button, I get the error "file Not found".

I uploaded the php file to my cgi-bin. Assuming that it works how do I view the information. sorry if I am asking toooooo stupid questions. This is new to me, but I want to try it out.
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post by DuFF »

The two files must be in the same folder and can be in any folder that you want. PHP files are not restricted to the cgi-bin. If you want to see all the information that is passed from the HTML form page to formresponse.php you could do this:

formresponse.php

Code: Select all

<?php
echo "<PRE>";
print_r($_POST);
echo "</PRE>";
?>
This will print all the information held in the $_POST superglobal array. It should look something like this:

Code: Select all

Array
(
    &#1111;name] =&gt; Will Duff
    &#1111;email] =&gt; duff@example.com
    &#1111;Place] =&gt; USA
    &#1111;website] =&gt; http://www.duffdesigns.net
    &#1111;comments] =&gt; PHP r0x0rs my b0x0rs!
    &#1111;Submit] =&gt; Submit
)
So if you did

Code: Select all

echo $_POST&#1111;'name'];
in the formresponse.php it would print out "Will Duff" on the screen. To understand this you may need to know the basics of arrays. Hope this helps :D
bengaluru
Forum Newbie
Posts: 6
Joined: Sun Dec 28, 2003 9:53 am
Location: Toronto

Post by bengaluru »

Great.

Thanks for all the information. sorry if my questions were too basic and stupid. I have read about arrays this morning.

Thanks and have a great holiday.
Post Reply