Page 1 of 1

PHP Form Troubles

Posted: Sun Apr 25, 2010 4:49 pm
by djhayman
Hello,

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;
?>
And here is the xhtml code creating the form and (attempting) error message display.

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 &copy; 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

Re: PHP Form Troubles

Posted: Sun Apr 25, 2010 11:57 pm
by Christopher
What is the specific problem you are having? What is your code not doing, or doing incorrectly? What lines do you think are the problem?

Re: PHP Form Troubles

Posted: Mon Apr 26, 2010 3:42 am
by djhayman
Hi there,

Cheers for your reply, I hope this helps.

The Code is:
Performing form validation,
Counting the number of errors,
Adding the error messages to a string

The code doesn't
Display the error messages once redirected
Email me when a form is entered and passes validation ( i think this is due to being tested on a localhost on os x )

When i have tested to see if the code is actually storing session variables by simply putting an echo command before the header command i get the results i want. However, after being redirected to the form page again, the error message do no display.

Thank you for any help.
Dan

Re: PHP Form Troubles

Posted: Mon Apr 26, 2010 12:01 pm
by Jonah Bron
I see syntax errors in your code. You should turn on error reporting, fix the errors you understand, and tell us what the rest say.

http://www.bradino.com/php/error-reporting/

Re: PHP Form Troubles

Posted: Tue Apr 27, 2010 4:53 pm
by djhayman
Hi there,

Do you mean errors reporting which occurs for example: whenever you submit a form and if you have missed out a "}" or a "," then it says something like phase error on line etc...

If so, I don't receive any, it simply uses header command appropriate to the validation of the form. However, if the form return invalid, the error messages are displayed.

If not, where can i find the error log. I am on os x using the built apache server.

Cheers for any help, sorry about my learning curve.

Dan