Hi, I am new to PHP. I've tried to create a form in HTML and a PHP page for validating the data entered into the form and adding them into a MySQL database. However, when i tried them out, it happens that after i press the submit button in the HTML page, it just show a blank page instead of what i expect. I've tried several times, but it is still the same and i don't know what is going wrong. So i hope that i can find some help here. Thank you.
here's the coding for the HTML page
<html>
<head>
<title>Candy Shop -- Comment Book</title>
</head>
<body>
<h2>Candy Shop's Comment Book</h2>
Welcome to Candy Shop's Comment Book.<br>
Please leave your comments about out shop or web site here.<br>
We appreciate your opinions and thank you for helping us improve.<br>
<form action='processform.php' method='POST'>
<table border=0>
<tr>
<td><b>Name :</b></td>
<td><input type='text' name='visitorName' size='40' maxlength='40'></td>
</tr>
<tr>
<td><b>E-mail Address :</b></td>
<td><input type='text' name='email' size='40' maxlength='60'></td>
</tr>
<tr>
<td><b>Comments :</b></td>
<td>
<textarea name='comments' cols='50' rows='8' wrap='virtualv'></textarea>
</td>
</tr>
<tr>
<td></td>
<td>* Please note that html codes are not allow.</td>
</tr>
<tr>
<td colspan='2' align=center><input type='submit' name='submit' value='Submit'></td>
</tr>
</table>
</form>
</body>
</html>
And this is the PHP file:
<?php
$visitorName=htmlspecialchars(trim($_POST['visitorName']));
$email=htmlspecialchars(trim($_POST['email']));
$comments=htmlspecialchars(trim($_POST['comments']));
$submit=$_POST['submit'];
$error=FALSE;
if($visitorName=="")
{
echo "You did not enter your name.";
$error=TRUE;
}
if($comments=="")
{
echo "You did not enter the comments.";
$error=TRUE;
}
if(!$email=="" && !eregi('/[\w_-.+]+@[\w_-]+(\.[\w_-])+$/',$email)
{
echo "The email entered does not look like a valid email.";
$error=TRUE;
}
if ($error)
{
echo "Please note the problem and go back to resubmit your comment."
echo "<a href="comment.html"><h4>Go back</h4></a>";
}
else
{
$user="username";
$host="hostname";
$password="password";
$database="candyshop";
$connection = mysql_connect($host,$user,$password)
or die("<h3>could not connect to server.</h3>\n Please try again later.");
$db = mysql_select_db($database)
or die("<h3>could not select the database.</h3>\n Please try again later.");
$query="insert into comment (visitorName,email,comments)values('$visitorName','$email','$comments')";
$result=mysql_query($query)
or die("Could not sent your comments.Please contact us.");
echo "Your comments are added. Thank you.";
}
?>
</body>
</html>