Building PHP Applications With Macromedia Dreamweaver MX - Naming Names (
Page 5 of 10 )
Once your site has been set up, it's time to get started with a simple
example - a page that display PHP environment settings via the phpinfo() function.
Dreamweaver provides you with an easy way to get started with building a PHP
script - pop open the File -> New menu, and select "Dynamic Page" and "PHP" from
the pre-defined templates.
You can now add the function call to phpinfo() in this page - switch to the code
view using the View -> Code menu option, and type the following code into your
document:
<?php phpinfo(); ?>
Save the file, and have Dreamweaver preview it for you via the toolbar icon (you
can also hit the F12 key) and a new browser window should pop up with the output
of the script.
If you're using PHP, you're going to be doing a lot of form processing - let's
see how you can speed up the process in Dreamweaver. First, create a simple, properly-formatted
HTML form using a combination of the "Tables" and "Forms" tabs on the "Insert"
panel - this form should contain two text input areas, one named "name" and the
other named "comment".
Add a couple of submit and reset button and the form is ready for action.
Here's the code:
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<form
action="this.php" method="post">
<table width="75%" border="0">
<tr>
<td>Name</td>
<td><input name="name" type="text"></td>
</tr>
<tr>
<td>Comment</td>
<td><textarea name="comment"
cols="" rows=""></textarea></td>
</tr>
<tr>
<td><input
name="submit" type="submit" value="Submit"></td>
<td><input name="Reset"
type="reset" value="Reset"></td>
</tr>
</table>
</form>
</body>
</html>
Now comes the interesting part - integrating PHP business logic into this example.
Click the "PHP" tab that should have appeared on your "Insert" panel, and take
a look at the functions it provides:
Pretty basic: you can insert PHP code to access POST, GET, cookie and session
variables, include() and require() files, and attach programming constructs like
"if" and "else" statements.
Now, you probably already know how PHP form processing typically works. A form
processing script contains two logical blocks - one displays the empty form and
the other processes the user input submitted. Therefore, the first thing to do
is to insert an "if" conditional statement at the top of the code - just click
on the "If" button and Dreamweaver will do it for you:
<?php if ?>
// form code
Next, you need to check if the form has been submitted or not, via the "submit"
form variable. Use the "Form Variables" button, and write some code around what
Dreamweaver inserts for you:
<?php if (!$HTTP_POST_VARS['submit']) { ?>
// form code
Once you've wrapped your empty form in the "if" branch of the conditional, you
need to add form processing logic to the "else" branch. Use the "else" button
on the toolbar, and let Dreamweaver insert it for you.
<?php } else { ?>
This "else" branch of the script is supposed to merely display the data entered
by the user. You can do this via the echo() command, also accessible via a button
on the PHP toolbar.
<?php echo $HTTP_POST_VARS['name']; ?>
Here's the final script:
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
</head>
<body>
<?php if (!$HTTP_POST_VARS['submit'])
{ ?>
<form action="this.php" method="post">
<table width="75%" border="0">
<tr>
<td>Name</td>
<td><input name="name" type="text"></td>
</tr>
<tr>
<td>Comment</td>
<td><textarea name="comment"
cols="" rows=""></textarea></td>
</tr>
<tr>
<td><input
name="submit" type="submit" value="Submit"></td>
<td><input name="Reset"
type="reset" value="Reset"></td>
</tr>
</table>
</form>
<?php
} else { ?>
<table width="75%" border="1" cellspacing="5" cellpadding="5">
<tr>
<td width="39%">Name</td>
<td width="61%"><?php
echo $HTTP_POST_VARS['name']; ?></td>
</tr>
<tr>
<td>Comments</td>
<td><?php echo $HTTP_POST_VARS['comment']; ?></td>
</tr>
</table>
<?php
} ?>
</body>
</html>
Now, you can easily test this code using the "Preview" button, as discussed earlier.
Here's the output:
It should be noted that Dreamweaver's attempt to make PHP scripting a point-and-click
exercise, as demonstrated via the example above, isn't really an unqualified success
- the functionality provided on the PHP toolbar is extremely limited, and most
developers would find it more efficient to type in PHP statements themselves,
rather than clicking their way through the buttons on the PHP tab. Don't be disappointed,
though - although the program fails at this task, it excels when it comes to building
database-driven dynamic pages (more on this on the next page).
Once you're happy with the way your script is working, you can transfer the file
to your remote server (named "cerberus" in this case). Pop open the "Files" panel
on the right side of the workspace, select the "local view", and use the arrow
buttons to transfer files from your local site to the remote site.
You can also perform transfers in the other direction, by changing to the "remote
view" and either putting or getting files to/from the server.