Using htmlArea and a Database to Maintain Content on a Website
If you wish to develop a website that others can contribute to, one option is to have text files sent as e-mail attachments, convert these to HTML and then upload the file. There is, however, a better way! If the people making submissions to your site are capable of using a word processor then things can be done much more efficiently. This article will show you how to use a free component called htmlArea and a database to handle the addition and display of content on your website. Some knowledge of HTML, databases and server-side scripts is assumed.
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:
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.