Hi I am working on a contact us form. I got the form working correctly, but when it validates it puts the error on a new page. How can I have the error message show on the form page?

HTML Code:

<form action="contact.php" method="post" id="contactform">
              <label for="name">Full Name <span class="red">*</span></label>
              <input id="name" name="name" class="text" />
              <label for="email">Your email <span class="red">*</span></label>
              <input id="email" name="email" class="text" />
              <label for="phone">Phone Number <span class="red">*</span></label>
              <input id="phone" name="phone" class="text" />
              <label for="company">Company</label>
              <input id="company" name="company" class="text" />
              <label for="topic">Subject<span class="red">*</span></label>
              <input id="topic" name="topic" class="text" />
              <label for="comments">Message <span class="red">*</span></label>
              <textarea id="comments" name="comments" rows="6" cols="50"></textarea>
            <li class="buttons">
              <input type="image" name="imageField" id="imageField" src="images/send.gif" />

The PHP code:

// Pick up the form data and assign it to variables
$name = check_input ($_POST['name'], "Please enter your name");
$email = check_input ($_POST['email'],"Please enter your email");
$phone = check_input ($_POST['phone'], "Please enter your phone number");
$company = $_POST['company'];
$topic = check_input ($_POST['topic'], "Please enter your subject");
$comments = check_input ($_POST['comments'], "Please enter your message");

function check_input($data, $problem='')
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    if (strlen($data) == 0)
 } else {
  return $data;

// Build the email (replace the address in the $to section with your own)
$to = 'email@email.com"';
$subject = "New message: $topic";
$message = "Email: $email \n Phone: $phone \n Company: $company \n $name said: $comments";
$headers = "From: $email";

// Send the mail using PHPs mail() function
mail($to, $subject, $message, $headers);

// Redirect
header("Location: success.html");
Member Avatar

You need to put all the form vars and the error into a session array and pass it back to the form via header().

I dont quite understand sorry. I am a beginner and this is one of my first few times using PHP..

place the error checking code in same page in which the form code is.

Member Avatar

place the error checking code in same page in which the form code is.

you can certainly do this, but it can cause problems on page reload/refresh - it will resubmit the form.

you can place your submitted vars into an array like this:


 $_SESSION['subvals'] = $_POST;
 //do your validation, then something like:
 if($errors == false){
   //no errors - do you success code
   $_SESSION['subvals']['error'] = $errors;
   header("Location: form.php");


your form page:


/*give your ALL form fields default values - even if they're blank. Give them the same name as the form field id:*/
$ff_username = "";
$ff_password = "";
$ff_email = "";
$ff_rememberme = ' checked = "checked"';

  extract($_SESSION['subvals']); //will overwrite any exisiting variables of same name
  if(!$_SESSION['subvals']['ff_rememberme'])$ff_rememberme = "";
<input id="ff_username" name="ff_username" type="text" value="<?php echo $ff_username;?>" />
<input id="ff_password" name="ff_password" type="password" value="<?php echo $ff_password;?>" />
<input id="ff_rememberme" name="ff_rememberme" type="checkbox"<?php echo $ff_rememberme;?>" />

THis is off the top of my head - not tested, but you get the idea I hope.