Chapter 4. Writing the PHP in Your First Program

This chapter will help you complete your first PHP program and learn some of the basic ideas in PHP, such as variables. Before we start, take a look back at the HTML page we created earlier. The HTML looked like the following:

Note

From now on, we are using a valid DOCTYPE and valid header throughout our web pages. This will get you into making all of your web pages follow the standard. Note that the pages work without a DOCTYPE, but it is very much recommended you use them.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>A Sample Form</title>
</head>

<body>
<h1>A Sample Form</h1>

<form action="" method="post">
<p>Name: <input type="text" name="fullname" />
Address: <input type="text" name="address" /></p>
<p>Comments: <textarea name="comments" cols="40" rows="2"></textarea></p>
<p><input type="submit" name="submitbutton" />
<input type="reset" name="resetbutton" /></p>
</form>

</body>
</html>

If you want to make the page pass the data directly back to itself, you can generally make it follow this template:

<form name="sampleform" method="post">
. . .
</form>

We just remove the action attribute, and the data from this web page is passed to itself! What does this mean? All the data typed into the form are stored in specific variables. These variables can be passed to any PHP page. Some pages might just display what the user typed, other pages might add two numbers, and so on. The most important thing to remember is that any PHP page must have the extension .php, not .html. Rename the page from Chapter 3 firstphpprogram.php.

If you want your form to send its data to a program called something.php, modify the action attribute so it looks like this:

<form name = "sampleForm"
   method = "post"
   action = "something.php">

Unfortunately, the page doesn’t do anything with the data yet because we haven’t put in any code that handles it! No PHP in this case is kind of like throwing a football over a wall and hoping someone is there to get it. Without PHP, there is no one to retrieve or use the ball because we haven’t created him. With PHP, we can design someone to be on the other side of the wall and tell him what to do with the ball.

How do you code PHP? Well, because this is your first experience, and because I go over all of this in depth later in this chapter, I give just a very quick layout here. When you insert <?php ?> tags around code, the web server immediately recognizes that it is PHP info and starts running the PHP program. After the code has run, the web server puts together the PHP and the HTML into one document that becomes the final web site that the user sees. So let’s add these brackets.

Add these PHP tags in directly after the <h1> section that says Sample Form. Now our page looks like this:

<body>
<h1>A Sample Form</h1>
<?php
?>
<br />
<form action="" method="post">
Name: <input type="text" name="fullname" />
Address: <input type="text" name="address" /><br /><br />
Comments: <textarea name="comments" cols="40" rows="2"></textarea><br /><br />
<input type="submit" name="submitbutton" />
<input type="reset" name="resetbutton" />

</form>
</body>

Note that <?php?> tags are not symmetrical like normal HTML tags. The web browser thinks of PHP as one gigantic tag! It also is good to note these other PHP tags you might see: <? ?> and <script language = "php"></script>. Loaded in the web browser it looks something like Figure 4.1.

Figure 4.1. The form with <?php ?> tags added.


Well, there is a reason why it looks the same: Nothing has really changed! We have added a placeholder for some PHP code, but we haven’t entered any code! Let’s do some basic stuff. Enter the following lines:

echo "$_POST[fullname]. You are from $_POST[address]. Your comments were
  $_POST[comments]. <br> ";

When you see the word echo in a PHP program, the following text is displayed on the screen. In this case, we are displaying the information inputted on the form. Do you remember when we set the form’s method to post? Well, all the data that the user typed in the form is put into a storage device called $_POST. See all those references to $_POST in the PHP code? The letters in the brackets immediately following $_POST access the contents of the form element that had that name in the HTML form. So, the user’s address was put into a textbox with the name set to address. Therefore, the user’s address is stored in $_POST[address]. The entire statement is echoed and the variables are displayed in place of the $_POST[]sections. When you put that into the source, your code will look like the following, and you can see the result in Figure 4.2:

Figure 4.2. The form with some PHP added.


<body>
<h1>A Sample Form</h1>
<?php
echo "$_POST[fullname]. You are from $_POST[address]. Your comments were
  $_POST[comments]. <br> "

?>
<br />
<form method="post">
Name: <input type="text" name="fullname" />
Address: <input type="text" name="address" /><br /><br />
Comments: <textarea name="comments" cols="40" rows="2"></textarea><br /><br />
<input type="submit" name="submitbutton" />

<input type="reset" name="resetbutton" />
</form>
</body>

Caution

If you run this code on a server with strict warnings turned on, you may get some strange errors about undefined indices. Adding an if(isset()) clause will fix this error. You can see how to do this later on in this same section, just before we start talking about variables.


You see some new words written! The variables haven’t been sent yet, so it just says . You are from . Your comments were .. Enter your name, address, and some comments into the form and press the submit button. Suddenly, the page looks like Figure 4.3. Cool, huh? Now it reflects exactly what was said to you! Figure 4.4 shows what the source looks like.

Figure 4.3. The form after being submitted.


Figure 4.4. The source of the form.


Why does the page show ’; ?>?

If you are following this program and try running the web page, there is a chance you might get an error in which none of the PHP code works. The reason is that you are not accessing the page correctly through your web server. PHP only works if it is accessed through the Apache web server. In general, you get the error if you double-click a web page to open it. If you find the web page by using localhost in your URL, you will get the correct page. Refer back to Chapter 1 to learn about installation and setting up your web server.


By the way, did you notice how annoying it was that the web form showed a sentence with blanks? Well, check out what we can do easily with PHP. Using a little bit of code, we can make the text disappear when the page is first seen, and reappear when the page is submitted. We need to change the PHP code that we designed earlier to look like this:

<?php
if (isset($_POST['name'])){

echo "$_POST[fullname]. You are from $_POST[address]. Your comments were
  $_POST[comments]. <br> "

}
?>

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.15.15