|
|
|
|
![]() ![]() |
Apr 13 2007, 03:05 AM
Post
#1
|
|
|
Super Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 397 Joined: 9-June 06 From: Wisconsin Member No.: 24,924 |
Intro To PHP
Tutorial 3 - Form Data and Conditional Statements Released 4/12/07 By Chris Feilbach aka GhostRider Contact Info: E-mail: assembler7@gmail.com AIM: emptybinder78 Yahoo: drunkonmarshmellows Website: http://www.ghostrider.trap17.com PART I - FORM DATA One of the things that makes PHP so popular is its ability to handle form data. You can do whatever you want with it, you can email it to yourself, store it in a database, display it on a page, you can do literally ANYTHING with it. There are two ways to send data to your server, GET and POST. Both are equally easy to use in PHP. GET is the stuff after the question mark in the address bar in the browser. You don't see the POST data in the address bar, it is sent discreetly along with the request for the webpage. POST is more secure than GET, and can also handle more data being sent. I only use GET when I am splitting my page into different sections and I don't want to make a bunch of PHP pages. I will go into that more in a different tutorial. http://www.ghostrider.trap17.com/index.php...=view&aid=1 The above address has three GET variables, mode, submode, and aid. Now how do you use this data in your PHP script? Look below. http://www.ghostrider.trap17.com/index.php...=view&aid=1 CODE <?PHP // For GET data. $var1 = $_GET['mode']; $var2 = $_GET['submode']; $var3 = $_GET['aid']; PHP?> If the variable was sent via POST, you would change all the $_GET['']s to $_POST['']s. CODE <?PHP // For POST data. $var1 = $_POST['mode']; $var2 = $_POST['submode']; $var3 = $_POST['aid']; PHP?> We will be creating two pages in this tutorial, one that sends data, and the other that processes the data and displays it. First, lets look at the html required to make this work. We are going to make two pages: index.php - Asks user for name and sex, and sends it to process.php. process.php - Processes the user's data and displays it. Index.php will be sort of like a welcome to our site. We ask them they're name and sex. First we need to take care of the basics and make our site look nice. CODE <html> <head> <title>Welcome to our site!</title> </head> <body> <b>Welcome to our site!</b> <br>We would like to know more about you. Please fill in the information below. We need to tell the browser where to send the data. We will send it via POST because it is more secure than GET. We are sending to process.php (our action) and using POST (our method). CODE <form action='process.php' method='post'> If you remember your HTML, you will know that each input has a type, a name, and usually a value (the exception are text boxes). They also sometimes have other variables that change the way they behave on the page. We care about two, the name, and the value. Lets ask our user for their name. CODE <br><br>Your name:<input type='text' name='username' size=50 maxlength=50> Lets ask them for their sex also. Remember a radio button is the same as an option. Radio buttons that are in a group always have the same name, but different values. CODE <br>Your sex: <br><input type='radio' name='sex' value='m'>Male <br><input type='radio' name='sex' value='f'>Female <br><input type='radio' name='sex' value='u'>Undecided Lastly, put in the submit button. The value tag in submit sets the buttons caption (what it displays). CODE <br><input type='submit' value='Submit!'> For the sake of being proper with our coding, we'll end the form and the body. CODE </form> </body> </html> All put together the code looks like: CODE <html> <head> <title>Welcome to our site!</title> </head> <body> <b>Welcome to our site!</b> <br>We would like to know more about you. Please fill the information below. <form action='process.php' method='post'> <br><br>Your name:<input type='text' name='username' size=50 maxlength=50> <br>Your sex: <br><input type='radio' name='sex' value='m'>Male <br><input type='radio' name='sex' value='f'>Female <br><input type='radio' name='sex' value='u'>Undecided <br><input type='submit' value='Submit!'> </form> </body> </html> PART II - Conditional Statements Conditional statements, which are also sometimes called if ... then statements, or simply if statements, are what makes a language truly dynamic. They allow whether your data is equal to something, or not equal. If you are dealing with numbers, you can also use greater than, less than, greater than or equal to, or less than or equal to. We are dealing with strings so we are only concerned with the top two. All condition statements start with an if. They then follow by a left parenthesis (. After that they have a variable, a sign (equal to, not equal to etc.), and either another variable or a constant (like "yes" or 2.5 or "hello"). They then have a right parenthesis ) and a left curly parenthesis {. The code you type between will be executed if the condition is true. The statement ends with a right curly parenthesis }. You should always indent your conditional statements because otherwise when your code becomes complex it will be NEARLY IMPOSSIBLE to debug or read. Signs that are used in PHP. == - Equal != - Not equal < - Less than > - Greater than <= - Less than or equal to >= - Greater than or eqaul to Here is an example conditional statement: CODE if ($variable == "yes") { // This code is executed if $variable is equal to yes. print "yes"; } Almost always you need to check for more than one variable. You can expand on your conditional statements by using an elseif statement. Its syntax is identical to an if statement. Below is the code: CODE if ($variable == "yes") { // This code is executed if $variable is equal to yes. print "yes"; } // You need to close the if statement. elseif ($variable == "no") { // This code is executed if $variable is equal to no. print "no"; Sometimes you can have three or four or even five different conditions to test for. You can use an else statement in one of two ways, you may use it in place of an elseif statement, or use it without a variable as a last resort. CODE // Using the else statement in place of an elseif statement. if ($variable == "yes") { // This code is executed if $variable is equal to yes. print "yes"; } else ($variable == "no") { // This else could also be an elseif statement. // This code is executed if $variable is equal to no. print "no"; } else ($variable == "maybe") { print "maybe"; } If you choose to use an else as a sort of "last resort", for an unknown value, use it like this. CODE // Using the else statement as a "last resort". if ($variable == "yes") { // This code is executed if $variable is equal to yes. print "yes"; } elseif ($variable == "no") { print "no"; } elseif ($variable == "maybe") { print "maybe"; } else { print "I have no idea what " . '$variable' . " equals."; // Notice that $variable had to be put in single quotes to have it display $variable, NOT its value. } Read through it a couple times until you really know it well! Conditional statements are sort of complex for beginners and should be reviewed as they are very important. One thing that helps when you start to develop your own software is to comment what you are testing for in your conditional statements. I still do that for some of my complex ones. It is a helpful tool and will reinforce what I am teaching you now. Now that we have covered conditional statements we can start to work on process.php. Whenever I have more than one file, I always write a brief comment that tells what the file name is and what it does. I would have done this in index.php but its all HTML. I also comment what form data it receives (if any), and how it receives it. This is great for debugging, and looking back on your code. I'm going to type this all in one, and comment it very well. Contact me if you have any questions about it. CODE <?PHP // process.php - Takes data from index.php and displays it // Form data (POST) // username - The name of the user. // sex - Contains the sex of the user (m=male,f=female,u=undecided) // We want to display the name of the user in the title and the body. If the user is a male lets make the font color blue, if they are female make it red, and leave it black if they are undecided. print("<html><head><title>Welcome "); // Lets get the data. Place the name inside of the $_POST[] or $_GET[] variable to get the value of that data. $username = $_POST['username']; print $username . "</title></head><body>"; // Prints the user name and finishes the head of process.php. Starts the body. $sex = $_POST['sex']; // Gets the sex of the user. if ($sex == "m") { // The user is a male. print("<font color='#0000FF'>You are a male.</font>"); } elseif ($sex == "f") { // The user is a female. print("<font color='#FF0000'>You are a female.</font>"); } else { print("You did not specify a sex."); } PHP?> </body></html> This is an awful lot to introduce so early in a tutorial, but both of these concepts go hand in hand. Read through it a couple of times. And once you understand congratulate yourself. You know one of the most important concepts in PHP. Be sure to contact me if you have any questions. The code demonstrated in this tutorial may be viewed at: http://www.ghostrider.trap17.com/tut/php3 |
|
|
|
Apr 13 2007, 10:20 AM
Post
#2
|
|
|
A clever man learns from his own mistakes, a WISE man learns from those of OTHERS ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: [HOSTED] Posts: 983 Joined: 12-April 06 From: Essex, UK Member No.: 21,719 |
Hey, another nice tutorial there, youve covered everything there is to know about IF functions and i think that anyone who follows your tutorials will easily be able to make useful scripts from what they have learned here.
i hope you keep up the good work! IF functions can be very hard to grasp as a beginner to programming but i think the way you explained it is very easy to understand |
|
|
|
Apr 13 2007, 03:31 PM
Post
#3
|
|
|
Advanced Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 144 Joined: 22-March 07 Member No.: 40,472 |
wow one nice tutorial here.
ceep up the good work m8 i'm looking forward to see more of your tutorials. thanx This post has been edited by Blessed: Apr 13 2007, 03:31 PM |
|
|
|
![]() ![]() |
Similar Topics
| Topics | Topics | |
|---|---|---|
|
|
|
|
Lo-Fi Version | Time is now: 7th September 2008 - 01:32 PM |