Using PHP
PHP is a scripting language you can use to make your web sites interactive. PHP
can do things that regular HTML web pages can not. You will need to learn how to
create web pages in HTML first before you can learn how to use PHP. To learn
more about creating HTML web pages, please see
Create A Web Page.
To use PHP, you need to first have a CGI account set up. You can not use .php
pages on your normal web server. For information, please see
How To Request A CGI Account.
Creating and Uploading a .php page
To create a PHP page, you simply type the code into a plain text document. Then
you save it as filename.php to your hard drive. Replace filename
with any name you wish. In order to actually use the php page and see what it
will do, you need to upload it to your CGI server. Please see
Upload A Web Page for
information (you will just replace ftp.yourdomain.com with
cgi.yourdomain.com and use your CGI Password instead of your
FTP Password). After the file is on your CGI server, you can access it
with your web browser to see it work. The address to the new file would be
something similar to:
http://cgi.yourdomain.com/filename.php
Where you replace yourdomain.com with your actual domain name and filename
with the name of the php file you uploaded.
The Basics
Let's get started! First, it is important to realize that PHP code can be
embedded in with HTML code. This means you can have a mixture of HTML
and PHP code in the same page. You can also embed HTML code within PHP
code. Because of this versatility, you can create just about any type of
interactive web page you can dream of. Let's look at a simple PHP page:
<html>
<head>
<title>Hello World</title>
</head>
<body>
<?php
$hello_variable = "Hello World!<br>";
echo $hello_variable;
echo "This completes the test.";
?>
</body>
</html>
Notice the highlighted area. The first line is a <?php which tells
Active Web Hosting's server to parse this information through the php scripting
parser before sending it to the visitor's web browser. The php parser then
runs the code in the block until it sees a ?> in which it stops and let's the
web browser finish the rest. The resulting code will be send to the web browser
and will look like this:
<html>
<head>
<title>Hello World</title>
</head>
<body>
Hello World!<br>This completes the test.
</body>
</html>
So the server substitutes the result of the PHP code before it
gets sent to the visitor's web browser.
Looking back at the code example, you'll see the next line as
$hello_variable = "Hello World!<br>"; This is called a variable.
Variables store text or numbers so you can use them in other areas of your
script. A variable name can be any group of letters and numbers, as long as the
variable name does not start with a number or include special characters such as
punctuation. All variables usually start with a $ though there are some
exceptions in more technical code which is beyond the scope of this tutorial.
In the quotation marks you put a string, which is another fancy word for
text. Variables can also store numbers, but instead of being enclosed in
quotation marks, they are enclosed in single quotes, such as $hello_variable = '3';
You'll also notice that every statement ends with a semi-colon.
Next we use the echo statement in two different ways. The first way, we
tell echo to print out in the browser page the words "Hello World<br>".
Notice we also used HTML code in this to create a line break. You can embed
HTML code in an echo statement. The second way you just put whatever text you
want to print in quotation marks.
The result of this will be the browser showing the following text:
Hello World!
This completes the test.
Using PHP to Read Forms
Believe it or not, that's about all you need to know about PHP to create a simple,
form! Let's take a look at the code. This may seem a little complicated at first,
but we'll go through it step-by-step and you'll see it's acutally quite easy.
<html>
<head>
<title>Color Chooser</title>
</head>
<body>
As you can see, we start off just like we do any other HTML page. Notice that
some lines start with a // and a description. These are called comments
and they can appear anywhere in your php code. If you want to put comments in
any HTML section of the page, you still must use <!-- and --> to enclose
the comment. Be careful to know which section you are in and use the appropriate
comment type.
<?php
// No input yet, please fill in form.
if ($HTTP_POST_VARS['done']){
// Read the form data into variables
$name = $HTTP_POST_VARS['visitor_name'];
$color = $HTTP_POST_VARS['color'];
Here we are using an if statement to start a block of code. We can put
anything we want within an if block. The if statement usually goes
like this:
if (test) {
Things to do if the test is true.
} else {
Things to do if the test is false.
}
The else part of the statement can be omitted if needed. In the above
example, we test the form's hidden variable done to be sure that the
form was actually submitted. This was in the form code below as:
<input type="hidden" value="done">
You'll see we refer to form variables as $HTTP_POST_VARS['value'] due to
the fact we used the method="post" in the <form> tag below. This is the
preferred way to get data from a form.
If the form is filled out, then we store the value from the name="visitor_name"
or Name: field in the form into the $name variable. We also do
this for the $color variable.
if($name){
printf ("Your name is %s and your color choice was %s.", $name, $color);
}else{
echo "You must at least fill in your name.";
echo 'Try again <a href="http://cgi.yourdomain.com/yourform.php">here</a>';
}
Here we insert another test. You can insert tests and loops inside other if test
statements if you want. The above code makes sure someone filled in their name.
This is good for double-checking form input. If their name was filled out then
the printf function prints out their name and color choice, otherwise
the script tells the user to try again and gives them a place to return to.
Note that in a printf function, you enclose the parameters, meaning the
format string, and variables in that order within parenthesis. This is the normal
way to send data to a function. The %s means to print whatever is in the next
encountered variable. The first %s means to insert in that space the contents of
the varialbe $name and the second %s means to insert in that space the
contents of the variable $color.
// Here is the form visitors fill out.
} else {
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']?>">
<input type="hidden" value="done">
<table border="0">
<tr><td align="right" valign="top">Name:</td>
<td><input type="text" name="visitor_name" size="50"></td></tr>
<tr><td align="right" valign="top">Color Choice:</td>
<td><select name="color">
<option>Red</option>
<option>Green</option>
<option>Blue</option>
<option>White</option>
</select></td>
</tr><tr>
<td align="center" colspan="2">
<input type="submit" value="Send">
<input type="reset">
</td></tr></table>
</form>
Now you see we end the php code after an else. This does two things. The
} else { means that if the form wasn't filled out yet, then to display
the form. Then the ?> says that the following code will be in normal
HTML.
Look at the following line after that:
<form method="post" action="<?php echo $_SERVER['PHP_SELF']?>
This line again mixes PHP and HTML and even within and HTML tag! This
part <?php echo $_SERVER['PHP_SELF']?> says to use this
very same page to look at the form data. This means that this page can
be used for two things. Remember we were testing for the eventuality of the form
actually being filled out. After the form's Send button is pressed, then
this .php page will be run again but with the data from the form sent
to it. If the data is encountered, then that is when the first part of our first
if statement will be used.
<?php
}
?>
</body>
</html>
Naturally, once we are done with the code, we end it. The above may seem a bit
confusing, but remember, we were using HTML for the form in an if statement.
So we have to go back to PHP mode, end the if statement with the } and then
we go back to HTML mode to close the body and html tags.
So, in summary, what this page does does is:
if data from a form on this page has been detected, then
if the name has been filled in, then
print out the user's name and their color choice.
else
Tell the user to fill in their name and give a link back to this page.
End of second if to test name.
else
Print out the form and let the user make their choices.
End of first if to test whether there was form data sent or not.
As you can see, we can use one PHP page to process form data and not need
a web page and a separate CGI script to do it. The entire source code is
provided below:
For another very useful example, please see
Using PHP To Send Email From A Form.
For more information and other tutorials and resources, please see
More Information About CGI, Perl, and Other Scripting Languages.
|