Me Pez Me Pez



Home| Links Info| Partners| About Us| Listings
Register | Login  | Tutorials| Policies| Contact Us

 !    Tutorial Three - PHP Component of a Simple Form

Tutorial 3 - Part II -Flying into Formation

In tutorial 2, Part I, you built a form in HTML. Now you will build the execution, or action, for that form in PHP. In PERL, you would do this on a separate page to which you would post the input of your form. In PHP, you have a choice. You can do the same. You can post the input to a separate page. Or you can simply post it to the same page (which is what I do here). To do this, you will have to change your page from HTML to PHP. (You can also write your PHP on a separate page and 'include' it on your form page. But you would still have to change your form page from HTML to PHP.) So, if you've saved your form as "comment.html" and checked it out, save it again now as "comment.php". (One point I'd like to make here: PHP can be stricter than regular HTML. If you left out your body tag, or in fact, all of your HTML tags, your form still shows up in HTML in most browsers. This may not happen in PHP. In PHP, you may have to, at least, include an opening body tag. So if your form doesn't work right, include an opening body tag ("<body>") as shown in the snippet of code below.)

Now, before you get started, you need to figure what you want. You also need to figure out what you intend to do. First, where do you want your input to go: to a file, to a database, or just emailed to you? For this tutorial, we'll email the form's input to you and maybe save some of it to a file somewhere. After you learn a little MySQL (next tutorial), you can learn to save the input to a database.

Next question: do you want to check your guest's inputs? Do you need to be cautious about inserting your input data into a database? For this tutorial, you won't be inserting data into a database. So, for this tutorial, the answer is no. Then do you need to be cautious about inserting your input data into an email? For now, to start, let's assume that you don't. So is there still some reason to check your input data? Do you need to check to see if the email address given is a real email address? Or do you just want to check whether your guests filled in an email address or left it empty? Or does it matter? Can you give your guests a response, if necessary, without a valid email? Should you refuse a guest's comment just because they enter a bad email address?

For this tutorial, I'm going to assume that you want to see the guest's comment - no matter what. If your guests give you a bad email address, you might want to say that you received their comment but that the email they supplied was not valid. If your guests want to receive an email response, they will have to re-enter a valid email address.

So how would we begin to do any of this in PHP? To begin, since our php is on the same page as our form, we need to check to see if the form was filled out and submitted by someone. So on the top of your page of code, above the DOCTYPE, add the following php code: (Remember to include opening php and body tags - underlined below - if needed.)

submit_isset_full







The echo statement is only temporary. It's just a test to show you that your submit-isset statement was written correctly. (This is bad form to have just an opening body bag. But since you're the only one who will see this, you can probably get away with it - for now.)

The 'else' statement is included above because, if the form wasn't submitted, then you want to show the form. So the second thing you need to do is to close your php with a closing bracket ( } ). So go down to the bottom of your page and after your closing html tag, add the following php code to close the bracket ( } ) in your submit-if statement:

submit_isset_end






At this point, your comment.php should look like this:

<?php
if (isset($_POST['submit'])) { // if the form was submitted.
echo ('<p>Form was submitted.</p>');

} else {
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<title>Guest Comment</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
</head>

<body>

<table bgcolor="#000000" align="center">
<tr><td colspan="2" align="center"><h2><font color="#FFFFFF">Guest Comment</font></h2></td></tr>
<form name="guest_comment_form" action="comment1.php" method="post">
<tr><td align="right"><font color="#FFFFFF">First Name</td>
<td align="left"><input type="text" name="firstname" size="25" maxsize="20"> </td></tr><tr>
<td align="right"> <font color="#FFFFFF">Last Name</td>
<td align="left"><input type="text" name="lastname" size="25" maxsize="20"></td></tr>
<tr><td align="right"><font color="#FFFFFF">Email Address</td>
<td align="left"><input type="text" name="email" size="25" maxsize="60""></td></tr>
<tr><td align="right"><font color="#FFFFFF">Comments or Questions</td>
<td align="left"><textarea name="comments" rows="4" cols="40"></textarea></td></tr>
<tr><td align="center" colspan="2"><input type="submit" name="submit" value="Submit!">
<input type="reset" name="Reset" value="Reset"></td>
</tr>
</table>
</form>
</body></html>
<?php
}
?>


In your case, your form's action will reference comment.php ( action="comment.php" ), unlike the above code which references "comment1.php". And again your colors should be reversed and you may or may not use a maxsize in defining your inputs. But the rest of your comment.php should agree with the above code. If it does, save your "comment.php" and load it up to the public_html portion of your web site and view it in your browser. Press the submit button. If you wrote your submit-isset statement correctly, you should see the words: "Form was submitted." See this example. If this didn't happen, recheck the above code to make sure you copied it correctly. Also make sure that the name of your 'submit' button is 'submit' and not 'Submit' or 'SUBMIT.' Your submit button name has to agree exactly with the 'submit' in your submit isset statement. If all else fails, you can always just copy the box of code above by highlighting it and pressing "edit & copy" on your browser. Remember to save it as "comment.php" and change the form's action from "comment1.php" to 'comment.php."


If your 'submit-if' statement worked, or if you just decided to copy the above coded box and you are ready to move on, continue on page 2 >>



Flying Into Formation - Part II - pages . . . 1 . . . 2 . . . 3 . . . 4 . . . 5 . . . 6 . . . 7 . . .

Your IP address is: 38.103.63.17
 !   Sponsors

We'd like to thank our sponsors for their encouragement and support. If you can use their services, please support us by supporting them.


 !   Dev Shed
Dev Shed - The Open Source Web Development Site Dev Shed
Powered By Dev Shed

More >>
 !   Dev Shed Forum
Dev Shed - The Open Source Web Development Site Dev Shed
Powered By Dev Shed

More >>
 !   Tutorials





Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
More >>

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
More >>

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
More >>

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
More >>

adsense_anim
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
More >>

©2005 ME PEZ BLOGSNOVELSESCAPESHOMEFAQsPOLICIES
REGISTERLOGINFORGOT YOUR PASSWORD?