Loading...


bookmark - Php Form Data And Conditional Statements My third tutorial

Php Form Data And Conditional Statements - My third tutorial

 
 Discussion by ghostrider with 6 Replies.
 Last Update: February 19, 2012, 9:55 am
 
bookmark - Php Form Data And Conditional Statements My third tutorial  
Quickly Post to Php Form Data And Conditional Statements My third tutorial w/o signup Share Info about Php Form Data And Conditional Statements My third tutorial using Facebook, Twitter etc. email your friend about Php Form Data And Conditional Statements My third tutorial Print
Reply / Comment New Discussion / Topic Share / Bookmark E-Mail a Friend Print

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

   Thu Apr 12, 2007    Reply         

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









   Fri Apr 13, 2007    Reply         

<?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;Print $username . "</title></head><body>"; // Prints the user name and finishes the head of process.Php. Starts the body.$sex = $_POST; // 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>

   Fri Jul 3, 2009    Reply         


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:

CODE


<?PHP
if ($variable == "Y") {
header('Location: http://www.WebsiteA.Com');
} else {
header('Location: http://www.WebsiteB.Com');
?>

Can you help me create a PHP script that works.
George
-reply by George

   Tue Dec 15, 2009    Reply         

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

   Sat Apr 24, 2010    Reply         

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


CODE

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

   Mon Feb 13, 2012    Reply         


QUOTE (Sam)

If you guys are looking to load a new page with php.. I would suggest just using java.
Link: view Post: 514019


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.

   Sun Feb 19, 2012    Reply         

Quickly Post to Php Form Data And Conditional Statements My third tutorial w/o signup Share Info about Php Form Data And Conditional Statements My third tutorial using Facebook, Twitter etc. email your friend about Php Form Data And Conditional Statements My third tutorial Print
Reply / Comment New Discussion / Topic Share / Bookmark E-Mail a Friend Print

Similar Topics:

Php Formmail Generator

PHP FormMail Generator QUOTEPHP FormMail Generator - A tool to create ready-to-use web forms in a flash. Once the form has been generated, you have a full functional web form. Including error checking of required fields, email address validation, credit card number & expiry date checking ...more

   22-Apr-2005    Reply         

A Practical Application Of Get And ...

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: [url="http://www.g ...more

   20-Apr-2007    Reply         

Can You Suggest A Beginner Php Tuto...

Hello members.. I heard from my friends that PHP is a good scripting language for Web based application.Can you suggest one good beginner PHP tutorial that is using database as mySQL.So please suggest me.. Most of the hosting websites allows free hosting for PHP and mySQL.. please ...more

   27-Jul-2009    Reply         

Driver Verifier    Driver Verifier (3) (1) Arrays In Php My Fourth PHP Tutorial  Arrays In Php My Fourth PHP Tutorial