I was wondering if anyone could help me out with my PHP form troubles.
The php code I have so far takes values from a form, performs some validation. I am eager to introduce a system that highlights any invalid form fields to the user. So far the code contains an error counting system and the ability to generate a strong of error messages. By using echo commands i am confident that this system works however, I am having trouble displaying these messages once being returned to the form page.
I have employed session variables for the error strings and counting. Moreover, the form data is not intended for a database but i am planing on using the mail command.
Here is the php validation code
Code: Select all
<?php
session_start();
//Session variable to keep count of the number of errors
$_SESSION['errCount'] = 0;
//Create and initialise session variables to any errors
$_SESSION['errFirstName'] ="";
$_SESSION['errphone'] = "";
$_SESSION["errEmail"] = "";
$_SESSION["errcontent"] = "";
//Store the form values as session variables and check them
$_SESSION['firstName'] = trim($_POST['fullname']);
// Validate the firstName
if (empty($_SESSION["firstName"])) {
// First name cannot be a null string
$_SESSION['errFirstName'] = "Please enter your name.";
$_SESSION['errCount'] = $_SESSION['errCount'] + 1;
}
$_SESSION['surname'] = trim($_POST['phone']);
// Validate the Surname
if (empty($_SESSION["surname"])) {
// the user's surname cannot be a null string
$_SESSION['errSurname'] = "Please enter your phone number.";
$_SESSION['errCount'] = $_SESSION['errCount'] + 1;
}
$_SESSION['email'] = trim($_POST['email']);
if (empty($_SESSION["email"])) {
// the user's email cannot be a null string
$_SESSION["errEmail"] = "Please enter your email address";
$_SESSION['errCount'] = $_SESSION['errCount'] + 1;
}
//Could also see if email was in correct format.
else if (!strpos($_SESSION["email"],"@")) {
$_SESSION["errEmail"] = "Please re-enter your email address.";
$_SESSION['errCount'] = $_SESSION['errCount'] + 1;
}
$_SESSION['firstName'] = trim($_POST['fullname']);
$_SESSION['content'] = trim($_POST['content']);
// Validate the comments
if (empty($_SESSION["content"])) {
// First name cannot be a null string
$_SESSION['errcontent'] = "Please enter a message";
$_SESSION['errCount'] = $_SESSION['errCount'] + 1;
}
// Now the script has finished the validation,
// check if there were any errors
if ($_SESSION['errCount'] > 0){
// There are errors. Relocate back to the client form
header("Location: ../contactus.html");
exit;
}
else {
// If we made it here, then the data is valid
// print "ALL OK TO PROGRESS";
$email_to = "djhayman02@gmail.com";
$email_subject = "TDTC Contact Form";
$email_message = "Name: $_SESSION[firstName].";
$email_message .= "Email: $_SESSION[email].";
$email_message .= "Telephone: $_SESSION[surname].";
$email_message .= "Comments: $_SESSION[content]";
// create email headers
$headers = 'From: '.$_SESSION['surname']."\r\n".
'Reply-To: '.$_SESSION['surname']."\r\n" .
//'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message);
}
//print $email_message;
header("Location:../thankyou.html");
exit;
?>Code: Select all
<?php
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Taunton Dog Training Club | Home</title>
<meta http-equiv="Content-Type" content="text/HTML; charset=UTF-8" />
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<div class="header">
<h1>Taunton Dog Training Club</h1>
<div class="nav"> <a href="index.html">home</a> <a href="venue.html">venue</a> <a href="classes.html">classes</a> <a href="howtojoin.html">how to join</a><a href="conditions.html">t&c</a><a href="#">contact us</a> </div>
</div>
<div class="upperleft">
<div class="upperright">
<div class="lowerleft">
<div class="lowerright">
<p class="text">
Please feel free to contact us using the form below if you have any unanswered queries.
</p>
<p>
<?php
//See if there are any erros in the Session Error Array
if (count($_SESSION['errCount']) != 0) {
echo "<font color=\"red\">Please amend your details below as required.
Items shown in red are
mandatory.</font>" ;
}
echo "$_SESSION['errCount']";?>
</p>
<form method="post" action="php/contactus4.php">
<fieldset>
<legend>Contact Us</legend>
<table>
<tr><td><label for="fullname">Full Name:</label></td><td><input type="text" name="fullname" id="fullname"size="20" maxlength="30" value="<?php echo $_SESSION['firstName']" /></td><td><?php echo "<font color=RED>".$_SESSION['errFirstName']."</font><br>";?></td></tr>
<tr><td><label for="email">Email Address:</label></td><td><input type="text" name="email" id="email" size="20" maxlength="35" /></td><td><?php echo "<font color=RED>".$_SESSION['errEmail']."</font><br>";?></td></tr>
<tr><td><label for="phone">Phone No.:</label></td><td><input type="text" name="phone" id="phone" size="20" maxlength="35" /></td><td><?php echo "<font color=RED>".$_SESSION['errSurname']."</font><br>";?></td></tr>
</table>
</fieldset>
<fieldset>
<legend><label for="message">Message</label></legend>
<?php echo "<font color=RED>".$_SESSION['errcontent']."</font><br>";?>
<p>
<textarea name="content" id="message" rows="4" cols="40"></textarea>
</p>
</fieldset>
<p>
<input type="submit" value="Submit" />
<input type="reset" value="Clear" />
</p>
</form>
<div class="footer">Copyright © Taunton Dog Training Club</div>
</div>
</div>
</div>
</div>
</body>
</html>
If any can help shed some light on this problem i would be very grateful.
Thanks in advance
Dan