Pages

Sunday, May 12, 2013

Write a PHP Script:

Now that you’ve got an HTML page sending information to sayHelloWeb.php, you
need to actually write that code. When you write PHP to run on the Web, it’s not
much different from the program you’ve already written (page 14). You have to get
information a little differently, because there’s no command line that a user can type
into. But other than that, things stay pretty much the same.
Open up a new text editor and type the PHP code shown here; it should look sort
of like an HTML-ized version of the sayHello.php program:
<html>
<head>
<link href="../../css/phpMM.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="header"><h1>PHP & MySQL: The Missing Manual</h1></div>
<div id="example">Example 1-1</div>
<div id="content">
<h1>Hello, <?php echo $_REQUEST['name']; ?></h1>
<p>Great to meet you. Welcome to the beginning of your PHP programming
odyssey.</p>
</form>
</div>
<div id="footer"></div>
</body>
</html>
Save this program as sayHelloWeb.php, and be sure you’ve got your file in plain
text with the right extension.
The first thing you probably noticed here is that this file looks a whole lot like HTML.
In fact, compared to sayHello.php, the first PHP program you wrote, this version
might look like a style of programming that’s a lot easier to learn. That’s because
once you’re using PHP to work and interact with web pages, a lot of what your PHP
programs will do is insert data into existing web pages, which means you’ll be working
with HTML a lot. Of course, that’s great news, because you already know HTML,
so you’ll be adding to what you know, rather than learning something completely
new from scratch.
Once you realize that a lot of this program is just HTML, you can probably already
guess what a lot of this program does. Here’s a section-by-section breakdown:
• The page starts out with a normal html element and head section.
• The body section begins, and sets up the page heading and example number,
just like the regular HTML page, sayHello.html.
• The page defines a heading with h1, and prints “Hello,.“
• The <?php tells the browser some PHP code is coming. Then, then $_REQUEST
variable is accessed, and a property called name within that variable is printed
using echo.
• The end of the PHP code is indicated with ?>.
• The rest of the HTML is output, just as in sayHello.html.
This program, like most PHP programs you’ll write, accepts its input from a web
page, either from one built in HTML like the pages you’ve created before, or from
another PHP program. It’s the job of that web page—sayHello.html in your case—to
get the user to enter her information, and then send that information on to this
program. The information from that HTML page is stored in $_REQUEST, which is a special variable in PHP.

No comments:

Post a Comment