Php Form Data And Conditional Statements - My third tutorial

free web hosting
Free Web Hosting, No Ads > CONTRIBUTE > Tutorials

Php Form Data And Conditional Statements - My third tutorial

ghostrider
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

 

 

 


Reply

shadowx
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 smile.gif

Reply

Blessed
wow one nice tutorial here.
ceep up the good work m8

i'm looking forward to see more of your tutorials.

thanx laugh.gif

Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Recent Queries:-
  1. php conditional using question mark - 15.66 hr back. (1)
  2. conditional form post php - 24.66 hr back. (1)
  3. php if "not equal" tutorial conditional - 26.43 hr back. (1)
  4. php tutorial send form data to email - 32.07 hr back. (1)
  5. conditional statements php not equal to - 35.83 hr back. (1)
  6. php conditional submit button - 44.95 hr back. (1)
  7. php forms data value - 58.36 hr back. (1)
  8. php forms conditional statements - 64.00 hr back. (1)
  9. condition statement for after post in php - 66.23 hr back. (1)
  10. grouping radio buttons html php form data - 68.50 hr back. (1)
  11. conditional html form - 71.36 hr back. (1)
  12. send form data php - 73.82 hr back. (1)
  13. how to save form data tutorial - 78.02 hr back. (1)
  14. excel conditional statements more than one - 81.98 hr back. (1)
Similar Topics

Keywords : php, form, data, conditional, statements

  1. Getting Started With Mysql
    creating tables and insert data into them. (2)
  2. How To Group Multiple Sets Of Data In Microsoft Excel 2007
    (0)
    How to Group Multiple Sets of Data in Microsoft Excel 2007 Sometimes you may open several
    workbooks and work with a number of the same workbooks at a time. You can open this group of files
    with Microsoft Excel 2007 simultaneously. But you have to define them as part of a workspace, and
    save them in a single Excel 2007 file. To do this, follow below steps: 1- Click On the View tab and
    then in the Window group click Save Workspace. The Save Workspace dialog box appears. 2- In the File
    name field, type your work name and then Click Save. 3- At the top-left of window, C....
  3. A Practical Application Of Get And Switch Statements
    My 6th PHP tutorial (1)
    Intro To PHP Tutorial 6 - A Practical Application Of GET And Switch Statements Released 4/20/07 By
    Chris Feilbach aka GhostRider Contact Info: E-mail: assembler7@gmail.com AIM: emptybinder78 Yahoo:
    drunkonmarshmellows Website: http://www.ghostrider.trap17.com Today I will teach you another
    important concept that you probably see a lot in webpages that you visit. Ever see something like
    "?id=22" or "?mode=login" in your address bar on your browser? If you remember back to the second
    tutorial you should recognize this as formdata, sent via GET. But you don't nee....
  4. Adding Data To A Database And Displaying It Later
    Using Forms, PHP and MySQL (1)
    Requirements: PHP Support MySQL Database access I am going to use a news program as an example.
    Ok, first you are going to need to connect to the database. Do so by using the code below. I have
    added some comments where you will need to edit to fit your server's specifications. Create a
    new file with notepad and call it config.php QUOTE //Change root to your database
    account's username $dbusername = "root"; //Add your account's password in between the
    quotations $password = " "; //Add the name of the database you are using in betw....

    1. Looking for php, form, data, conditional, statements

Searching Video's for php, form, data, conditional, statements
advertisement



Php Form Data And Conditional Statements - My third tutorial



 

 

 

 

ADD REPLY / Got an Opinion! a humble request :-) RAPID SEARCH! Free Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE