Auto-Generating the Entry Form
Listing 1 showed the entry form for your blog, which was just fine, except that it didn't have the facility to set the category. You might think that it is easy to just add another form field that contains a list of the categories and you can take it from there, but there would be a problem with this. You would need to hard-code the categories into the page, so if the database ever changed, then you would also need to go back and change your page. A far more efficient way of doing this is to have a PHP script generate the HTML for you. This is a lot easier than it sounds!
|Figure 7. Look Mom: It's your first PHP page.|
The first thing to do is to get an instance of your page and save it as a PHP file instead of an HTM file. (For this example I called it BlogForm.php.) You can then put it on your Web server by copying it to the 'www' directory. You can then browse to this form using the http://localhost/blogform.php
URL. The result is shown in Figure 7
. You've just written your first PHP page! Congratulations.
But hang on a second: You might think that all you did was write an HTML page and call
it a PHP page. Well you would be right. This is because the PHP parser handles HTML, and in fact works best in these cases by taking the HTML and allowing you to mark it up with information generated on the fly.
Consider the example, where you want to add category information to this page. You would put a form menu object on the page between the blog entry and the pin (see Figure 7
). The code for this object would look something like:
In PHP we can generate this HTML using this code:
<option value="<?php echo($lp); ?>">Test<?php echo($lp); ?></option>
At first this may look like gibberish but if you note that the code between the <? And the ?> is considered to be server code and the rest to be client side HTML, you will see it take shape.
First you output the <select> tag, which is identical to the category object code above. Then you go through a three-iteration loop, in which you output the <option> tag, with the value set to the loop variable, and the content to the string 'Test,' prefixing the loop variable. When you run the page in your browser you will see that the results are the same!
The next step is to read the values from the database and to loop through them, outputting the menu selector:
$dbconn = @mysql_connect('localhost', 'root', 'rootp');
die('Error connecting to DB!');
if (! @mysql_select_db('blog') )
die( '<p>Unable to locate the main ' .
'database at this time.</p>' );
$query = "SELECT * from Categories";
$result = @mysql_query($query);
while ($row = mysql_fetch_array($result))
<option value="<?php echo($row['ID']); ?>">
<?php echo($row['Description']); ?></option>
|Figure 8. What to Say?: The finished HTML form is driven by PHP.|
This snippet demonstrates using the @mysqlconnect
, and @mysql_fetch_array
commands to connect to a MySQL server, select a database on that server, run a query on that database, and load the resultset into an in-memory array, respectively. It then outputs the contents of that array to the selection box within a while loop.
This can then be added to the Form, to produce a data-driven form. This is available in the download as blogform.php (see Figure 8
Processing the Results
When you submit the form, the browser sends the value of the contents of its form controls to the destination as POST variables. This can be seen on the very first line of Listing 1, where the form method is POST and the destination is blogentr.php
The destination file receives these POST variables in the HTTP conversation, and as (in this case) it is PHP, it can retrieve them using the $_POST
array. The code to handle these variables and to use them to generate an INSERT
query that puts the information into the database is shown below:
$Headline = $_POST['tHeadline'];
$Body = $_POST['tBody'];
$Category = $_POST['sCategory'];
$PIN = $_POST['tPin'];
$SQL = "INSERT INTO blogs (category, headline, body) VALUES ('";
$SQL = $SQL . $Category . "','";
$SQL = $SQL . $Headline . "','";
$SQL = $SQL . $Body . "')" ;
The entire PHP script is available from the download
. This includes checks for connectivity to the database and valid SQL.