Jump to content
xisto Community
Sign in to follow this  
ghostrider

Php Form Data And Conditional Statements My third tutorial

Recommended Posts

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://forums.xisto.com/no_longer_exists/

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://forums.xisto.com/no_longer_exists/

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://forums.xisto.com/no_longer_exists/

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

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

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

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

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

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

<br><input type='submit' value='Submit!'>

For the sake of being proper with our coding, we'll end the form and the body.

</form></body></html>

All put together the code looks like:

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

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:

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.

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

// 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.
<?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://forums.xisto.com/no_longer_exists/

Share this post


Link to post
Share on other sites

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 ;)

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

If-then logic in PHP with referral to URLPhp Form Data And Conditional Statements

I have a PHP web form, which upon submission posts to a process.Php file. I would like to insert the following "if then" logic in the PHP file:

If FormParameter1=Y, then redirect to URL A

else, redirect to URL B

Here's the actual script I attempted to piece together:

<?PHPif ($variable == "Y") {header('Location: [url="https://websitea.com/"]http://WebsiteA.Com;);} else {header('Location: [url="https://websiteb.com/"]http://WebsiteB.Com;);?>
Can you help me create a PHP script that works.

George

-reply by George

Share this post


Link to post
Share on other sites
If-then logic in PHP with referral to URLPhp Form Data And Conditional Statements

 Hello George, I was having the same issue a few hours ago today, I am unsure if you have solved your issue but I was able to resolve the issue by using an "elseif" statement over the regular else statement. Not sure why this is but it resolved it.

-reply by Tracy Kellogg

Share this post


Link to post
Share on other sites

If you guys are looking to load a new page with php.. I would suggest just using java. i.e.:


if ($variable == "something"){echo("<a href=\"someurl\">Click Here</a>");echo("<script><!--location.replace(\"someurl\");--></script>");}

The first echo there in case java is disabled on a user's browser

Share this post


Link to post
Share on other sites

If you guys are looking to load a new page with php.. I would suggest just using java.

The code snippet that you have included in your post uses Javascript and not Java. The two are different - Javascript typically runs within the web browser (although there are instances of Javascript running on your computer, such as for scripting Windows, or using server-side Javascript) and Java typically runs on a server in the form of Java Server Pages (though you can run it within the browser as Applets or on your computer as a Java AWT/Swing/SWT application). Javascript runs as an interpreted language whereas Java is compiled into bytecode before execution.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.