Hi there.
I have a simple comment script. If people forget to type their name or something else in the required fields they get an error message stating that they must remember to provide the information necessary. However, when displaying the error message the form clears completely which is a bad thing - especially because people may have typed a long comment and then suddenly it's all gone. How can I make my form remember the input - even after error notifications - until you hit 'Send'?
My script has two parts. The script itself and the input form:
commentFunctions.inc.php:
<?PHP
/* TABLE DUMP:
DROP TABLE IF EXISTS `comments`;
CREATE TABLE `comments` (
`commentID` int(11) NOT NULL auto_increment,
`comment` text,
`URL` text,
`email` varchar(100) default NULL,
`name` varchar(40) default NULL,
`postNo` varchar(100) NOT NULL default '',
`theDate` datetime default NULL,
PRIMARY KEY (`commentID`)
) TYPE=MyISAM;
*/
//Config
// Mail is sent upon new comment
$GLOBALS['email'] = "your@e-mail.com";
// Window title
$GLOBALS['title'] = "Comments:";
// Full path to the comment script folder
$GLOBALS['root'] = "http://www.website.com/comments/";
//Db address (or leave empty.)
$GLOBALS['database_adress'] = "";
//Db name:
$GLOBALS['database_navn'] = "******";
//Username:
$GLOBALS['database_bruger'] = "******";
//Password:
$GLOBALS['database_password'] = "******";
// Functions
function writeCommentHeader() {
echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n";
echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"DTD/xhtml1-transitional.dtd\">\n";
echo "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"da\">\n";
echo "<head>\n";
echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\" />";
echo "<link href=\"/comments/comments.css\" rel=\"stylesheet\" type=\"text/css\" />\n";
echo "<title>Add or read comments</title>\n";
echo "<base target=\"_blank\" />\n";
echo "</head>\n";
echo "<body>\n\n";
}
function sendCommentMail($from, $body) {
$subject = "Ne comment from " . $from;
mail($GLOBALS['email'], $subject, $body, "From: no-reply@e-mail.com\r\n");
}
function insertComment($postNo) {
if (isset($_POST['theComment']) && isset($_POST['theName']) && isset($postNo)) {
if ($_POST['theComment'] != "" && $_POST['theName'] != "" && is_numeric($postNo)) {
$aComment = htmlspecialchars(mysql_escape_string($_POST['theComment']));
$aName = htmlspecialchars(mysql_escape_string($_POST['theName']));
$aURL = htmlspecialchars(mysql_escape_string($_POST['theURL']));
$anEmail = htmlspecialchars(mysql_escape_string($_POST['theEmail']));
sendCommentMail($_POST['theName'], $_POST['theComment']);
$query = ("INSERT INTO comments (comment, URL, email, name, postNo, theDate) VALUES ('$aComment', '$aURL', '$anEmail', '$aName', '" . $postNo . "', NOW())");
//executeSQL closes conn. to db.
executeSQL($query);
}
}
}
//Make links clickable:
function url2link($url,$chr_limit = 70,$add = '...') {
return preg_replace("!(http:/{2}[\w\.]{2,}[/\w\-\.\?\&\=\#]*)!e", "'<a href=\"\\1\" class=\"orange\" title=\"\\1\" target=\"_blank\">'.(strlen('\\1')>=$chr_limit ? substr('\\1',0,$chr_limit).'$add':'\\1').'</a>'", $url);
}
function getComments($postNo) {
setlocale(LC_ALL, 'da_DK.ISO_8859-1');
$comments = executeSQL("SELECT name, email, comment, URL, UNIX_TIMESTAMP(theDate) as thisDate FROM comments WHERE postNo = '" . $postNo . "' ORDER BY theDate");
//hvis der ikke er nogen kommentarer kører løkken ikke
while ($data = mysql_fetch_array($comments)) {
$localTime = (int)strftime("%e", $data['thisDate']) . strftime(". %b. %Y", $data['thisDate']) . " kl. " . strftime("%H:%M:%S", $data['thisDate']);
$comment = stripslashes($data['comment']);
$comment = nl2br($comment);
$comment = wordwrap($comment, 65, "\n", 1);
echo "<div class=\"box\">\n";
echo "<p class=\"date\">" . $localTime . "</p>\n";
echo "<p>" . $comment = url2link($comment) . "</p>";
echo "<span class=\"small\">Skrevet af</span><span class=\"titel\"><b>" . stripslashes($data['name']) . "</b></span>\n";
if ($data['email'] != "") {
echo " | <a class=\"plain\" title='Email' href=\"mailto:" . $data['email'] . "\"> @ </a>\n";
}
if ($data['URL'] != "") {
echo " | <a class=\"plain\" title='Website' href=\"" . $data['URL'] . "\"> www </a>\n";
}
echo "</div><br />\n\n";
}
}
function getNumOfComments($itemNo) {
$numOfComments = executeSQL("SELECT COUNT(postNo) AS numOfCom FROM comments WHERE postNo = '$itemNo'");
//One or more comments
$tag = "Comments";
$data = mysql_fetch_array($numOfComments);
if ($data['numOfCom'] == 1) {
$tag = "Comment";
}
echo "<a href=\"#\" onclick=\"kommentar=window.open('" . $GLOBALS['root'] . "comments.php?postNo=" . $itemNo . "','commentWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=470,height=510'); return false;\" title=\"Tilføj eller læs kommentarer\" class=\"orange\">" . $tag . " (" . $data['numOfCom'] . ")</a>";
}
function executeSQL($strSQL, $close="true") {
//Open conn.
$link = mysql_connect($GLOBALS['database_adress'], $GLOBALS['database_bruger'], $GLOBALS['database_password']);
if (!$link) {
//If no conn. the function ends
echo "<p>" . mysql_error() . "</p>";
}
//Choose database
else {
if(!mysql_select_db($GLOBALS['database_navn'], $link)) {
echo "<p>" . mysql_error() . "</p>";
}
//Make query
else {
$result = mysql_query($strSQL);
if (!$result) {
//Show error if any
echo "<p>" . mysql_error() . "</p>";
}
}
if ($close == "true") {
if (!mysql_close()) {
echo "<p>" . mysql_error() . "</p>";
}
}
return $result;
}
}//End executeSQL
?>
comments.php:
<?PHP
if (isset($_GET['postNo'])) {
$postNo = $_GET['postNo'];
} else {
$postNo = $_POST['postNo'];
}
include("commentFunctions.inc.php");
//writes a header
writeCommentHeader();
?>
<body onload="if(document.formname)document.formname.theName.focus()">
<h2>
<?PHP echo $GLOBALS['title']; ?>
</h2>
<?PHP
//Error messages:
if (isset($_POST['send'])) {
if ($_POST['theComment'] == "" || $_POST['theName'] == "") {
echo "<div class\"box\"><p class=\"error\">Please enter at least your name and a comment - otherwise what's the point pushing the button...</p></div>";
} else {
insertComment($postNo);
}
}
//Show comments with postNo
getComments($postNo);
?>
<br>
<br>
<form name="formname" action="<?PHP echo $_SERVER['PHP_SELF']; ?>" method="post" target="_top" style="display:inline; margin:0px;">
<b>
<span class="titel">Name:</span><span class="star">*</span><br />
<input name="theName" type="text" style="border:1px solid #999;" size="30" />
<br />
<span class="titel">E-mail:</span><br />
<input name="theEmail" type="text" style="border:1px solid #999;" size="30" />
<br />
<span class="titel">Website:</span> <br />
<input name="theURL" type="text" style="border:1px solid #999;" size="30" /> * <span style="font-size:9px;">= Skal udfyldes.</span>
<br />
<span class="titel">Comment:</span></b><span class="star">*</span><br />
<textarea name="theComment" style="border:1px solid #999;word-wrap:break-word;" rows="10" cols="65"></textarea>
<br />
<input type="hidden" name="postNo" value="<?PHP echo $postNo ?>" />
<input type="submit" style="font-size: 12px; width:110px; border: 1px solid #000000; background-color: #e9e9e9;cursor:pointer;" name="send" value="Add comment" />
<input type="reset" style="font-size: 12px; width:50px; border: 1px solid #000000; background-color: #e9e9e9;cursor:pointer;" value=" Clear " />
</form><br />
<br>
<br>
<table width="300" border="0" cellpadding="0" align="center">
<tr>
<td style="text-align:center;"><input name="button" type="button" onClick="window.close()" value="Close window" class="submit_knap"></td>
</tr>
</table>
</body>
</html>
Any solutions to this problem?
Thanks in advance.