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">
          <ol>
            <li>
              <label for="name">Full Name <span class="red">*</span></label>
              <input id="name" name="name" class="text" />
            </li>
            <li>
              <label for="email">Your email <span class="red">*</span></label>
              <input id="email" name="email" class="text" />
            </li>
            <li>
              <label for="phone">Phone Number <span class="red">*</span></label>
              <input id="phone" name="phone" class="text" />
            </li>
            <li>
              <label for="company">Company</label>
              <input id="company" name="company" class="text" />
            </li>
            <li>
              <label for="topic">Subject<span class="red">*</span></label>
              <input id="topic" name="topic" class="text" />
            </li>
            <li>
              <label for="comments">Message <span class="red">*</span></label>
              <textarea id="comments" name="comments" rows="6" cols="50"></textarea>
            </li>
            <li class="buttons">
              <input type="image" name="imageField" id="imageField" src="images/send.gif" />
            </li>
          </ol>
        </form>

The PHP code:

<?php
// 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)
    {
          die($problem);
 } 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_start();

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

}

your form page:

<?php
session_start();

/*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"';

if(isset($_SESSION['subvals']['error'])){
  extract($_SESSION['subvals']); //will overwrite any exisiting variables of same name
  if(!$_SESSION['subvals']['ff_rememberme'])$ff_rememberme = "";
}
if(isset($_SESSION['subvals']))unset($_SESSION['subvals']);
?>
...
<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.