Using htmlArea and a Database to Maintain Content on a Website - Saving Content
(Page 3 of 4 )
I don’t intend to discuss here how a form is submitted from a web page. If you’ve read this far, you probably know how to do this anyway. If not there are plenty of online tutorials available on this subject. Let’s assume that our htmlArea control is part of a “form” and the “action” attribute has been set to a file in the current directory called “processform.php”. For the sake of clarity, the topics used in the select control below have been hardcoded. In real-life these would most likely be created dynamically (or perhaps in this case magically) from a database. The HTML code for the form in your web page might look like the following:
<form name="frmitem" method="post" action=" processform.php">
<table border="0" >
<td>Title: </td><td><input type="text" name="title" maxlength=35></td>
</tr>
<td>Topic:</td>
<td><select name="topic">
<option>Ghosts</option>
<option>Goblins</option>
<option>Harry Potter</option>
<option>Haunted Places</option>
<option>Hermione</option>
<option>Ron</option>
</select></td>
</tr>
<tr>
<td colspan=2 valign ="top">Contents: </td></tr><tr>
<td colspan=2 ><textarea name="contents" rows="22" cols="40"></textarea></td>
</tr>
<tr>
<td align = "center" colspan="2">
<input type="submit" value="Send" > <input type="reset" value="Clear"></td>
</tr>
</table>
</form>
The “textarea” named “contents” will become an htmlArea control but will not appear differently in the source code. When this form is submitted it does not need to be handled in any special way. Any formatting done by the user will be captured as HTML code in your database.
Just as your form does not require any special coding, nor does your server-side script.
<?php
/* include files holding connection information and additional
functions */
include 'connection.php';
include 'dbfunctions.inc';
/***************************************************/
$user = "guest";
//retrieve information posted from form
$title=@$HTTP_POST_VARS["title"];
$description=@$HTTP_POST_VARS["description"];
$topic=@$HTTP_POST_VARS["topic"];
$contents=@$HTTP_POST_VARS["contents"];
//programmer created function from the dbfunctions.inc include file
opendatabase();
$strsql = "INSERT INTO items ".
"VALUES(null,'$title', $topic', ".
"'$user','$contents',null, 0)";
$connection = @ mysql_connect($hostname, $username,$password)
or die("Cannot connect to database");
if (! mysql_selectdb($databasename, $connection))
showerror();
if (!($result = @ mysql_query($strsql, $connection)))
header("Location: mainpage.php?status=F");
else
header("Location: mainpage.php?status=T");
? >
Again, for the sake of simplicity, we have used a literal for the value of the variable “$user” and have not verified any of the data submitted. Also, functions to open the database are not shown but are assumed to be in the “dbfunctions.inc” file.
Next: How to Display the Saved Contents >>
More HTML Articles
More By Peter Lavin