PHP Error
Posted by:
Marc Birch
Date: April 19, 2009 12:25PM
Hello,
I'm new to PHP and mySQL and I've been following a tutorial from phpf1.com (url:
http://www.phpf1.com/tutorial/php-form.html?page=1). I'm trying to make a simple form that's validated and then stored in a database. Though I keep encountering errors in the coding (I noticed some errors in the HTML, as I'm quite knowledgable with it) and I was wondering if anyone could could take a look at it?
<?php
// function to display form
function showForm($errorName = false, $errorEmail = false, $errorMesg = false) {
if ($errorName) $errorTextName = "Please enter your name!";
if ($errorEmail) $errorTextEmail = "Please enter a valid email address!";
if ($errorMesg) $errorTextMesg = "Please leave a longer message!";
echo '<form action="form.php" method="POST"><table>';
// display name field and error if required
echo '<tr><td>Name</td><td><input type="text" name="name"></td></tr>';
if ($errorName) echo "<tr><td colspan='2'>$errorTextName</td></tr>";
// display email field and error if required
echo '<tr><td>Email</td><td><input type="text" name="email"></td></tr>';
if ($errorEmail) echo "<tr><td colspan='2'>$errorTextEmail</td></tr>";
// display message field and error if required
echo '<tr><td>Message</td><td><textarea name="mesg"></textarea></td></tr>';
if ($errorMesg) echo "<tr><td colspan='2'>$errorTextMesg</td></tr>";
echo '<tr><td><input type="submit" name="SubmitForm" value="Send"></td></tr>';
echo '</form>';
if (!isset($_POST['SubmitForm'])) {
showForm();
} else {
// init error variables
$errorName = false;
$errorEmail = false;
$errorMesg = false;
$name = isset($_POST['name']) ? trim($_POST['name']) : '';
$email = isset($_POST['email']) ? trim($_POST['email']) : '';
$mesg = isset($_POST['mesg']) ? trim($_POST['mesg']) : '';
if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$";
if (strlen($name)<3) $errorName = true;
if (strlen($mesg)<10) $ errorMesg = true;
// display the form again as there was an error
if ($errorName || $errorEmail || $errorMesg) {
showForm($errorName, $errorEmail, $errorMesg);
} else {
echo 'Submission was successful!';
}
} // end else condition
} // end function
?>
Subject
Written By
Posted
PHP Error
April 19, 2009 12:25PM
Sorry, you can't reply to this topic. It has been closed.
Content reproduced on this site is the property of the respective copyright holders.
It is not reviewed in advance by Oracle and does not necessarily represent the opinion
of Oracle or any other party.