0

Okay, I tried once again, this time I removed the multiple php open/closing tags. So below is one big php chunk of code. If I fill out the form and send, the redirect works and I get the email - this all works great. The one last problem is the validation - I can submit empty fields and it redirects to the thankyou page - it doesn't warn users to fill out the fields...

So why now is the validation not working??? Thanks for your help guys.

<?php
// define variables and set to empty values
$fname = $lname = $email = $phone = $location = $size = $pvtype = $message = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
  $company = test_input($_POST["company"]);
  $fname = test_input($_POST["first-name"]);
  $lname = test_input($_POST["last-name"]);
  $email = test_input($_POST["email"]);
  $phone = test_input($_POST["phone"]);
  $address = test_input($_POST["address"]);
  $city = test_input($_POST["city"]);
  $provincestate = test_input($_POST["provincestate"]);
  $country = test_input($_POST["country"]);
  $location = test_input($_POST["location"]);
  $size = test_input($_POST["size"]);
  if(isset($_POST["type"])){ $type = $_POST['type'];}
  $message = test_input ($_POST["message"]);
}

function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}

// define variables and set to empty values
 $companyErr = $fnameErr = $lnameErr = $emailErr = $phoneErr = $addressErr = $cityErr = $provincestateErr = $countryErr = $locationErr = $sizeErr = $typeErr = $messageErr ="";
 $company = $fname = $lname = $email = $phone = $address = $city = $provincestate = $country = $location = $size = $type ="";

if ($_SERVER["REQUEST_METHOD"] == "POST") {

    if (empty($_POST["company"])) {
    $company = "";
  } else {
    $company = test_input($_POST["company"]);
  } 

   if (empty($_POST["first-name"])) {
    $fnameErr = "First name is required";
  } else {
    $fname = test_input($_POST["first-name"]);
    // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$fname)) {
      $fnameErr = "Only letters and white space allowed"; 
    }
  }     

  if (empty($_POST["last-name"])) {
    $lnameErr = "Last name is required";
  } else {
    $lname = test_input($_POST["last-name"]);
    // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$lname)) {
      $lnameErr = "Only letters allowed"; 
    }
  }

  if (empty($_POST["email"])) {
    $emailErr = "Email is required";
  } else {
    $email = test_input($_POST["email"]);
    // check if e-mail address is well-formed
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
      $emailErr = "Invalid email format"; 
    }
  }

  if (empty($_POST["phone"])) {
    $phoneErr = "Phone number is required";
  } else {
    $phone = test_input($_POST["phone"]);
    // check if phone number only contains 10 digits with no formatting
    if (!preg_match("/^[0-9]{10}+$/",$phone)) {
      $phoneErr = "Only enter a 10 digit number"; 
    }
  }

  if (empty($_POST["address"])) {
    $address = "";
  } else {
    $address = test_input($_POST["address"]);
  } 

   if (empty($_POST["city"])) {
    $city = "";
  } else {
    $city = test_input($_POST["city"]);
  }

  if (empty($_POST["provincestate"])) {
    $provincestate = "";
  } else {
    $provincestate = test_input($_POST["provincestate"]);
  }

  if (empty($_POST["country"])) {
    $country = "";
  } else {
    $country = test_input($_POST["country"]);
  }


  if (empty($_POST["location"])) {
    $locationErr = "Location is required";
  } else {
    $location = test_input($_POST["location"]);
    // check if location only contains letters
    if (!preg_match("/^[a-zA-Z ]*$/",$location)) {
      $locationErr = "Please enter a city"; 
    }
  }

 if (empty($_POST["size"])) {
    $sizeErr = "Please enter a number";
  } else {
    $size = test_input($_POST["size"]);
  }

  if (empty($_POST["type"])) {
    $typeErr = "Please select 1";
  } else {
    $type = test_input($_POST["type"]);
  }

   if (empty($_POST["message"])) {
    $message = "";
  } else {
    $message = test_input($_POST["message"]);
  }
}

$myemail = '[email protected]';//<-----Put Your email address here.

if ($_SERVER["REQUEST_METHOD"] == "POST") {

    $to = $myemail; 
    $email_subject = "Inquiry from: $fname $lname";
    $email_body = "You have received a new inquiry from:".
    "\n
     \n Name: $fname $lname \n Email: $email \n Phone Number: $phone
     \n Address: $address \n City: $city \n Province/State: $provincestate \n Country: $country
     \n I have a project in: $location \n The project type is: $type  \n The estimated project size is: $size
     \n Message: $message"; 

    $headers = "From: $myemail\n"; 
    $headers .= "Reply-To: $email";

    mail($to,$email_subject,$email_body,$headers);

    //redirect to the 'thank you' page
    header('Location: thankyou.html');
    exit();
}
?>
8
  • 1
    "the obvious output issue" I know it's obvious but can you point it out? Commented Jan 19, 2015 at 16:52
  • I will pay somebody to help me get this sorted once and for all. I have spent too much time on it and I know nothing of PHP - all the tutorials in the world I have found do not help me. Commented Jan 19, 2015 at 16:54
  • I thought it was obvious because of its location in the code??? I am new to dev. Commented Jan 19, 2015 at 16:55
  • I don't know .. I see 300 lines of code. What am I looking for? Commented Jan 19, 2015 at 16:56
  • I've scrolled the thing down and up twice. Just noticed that you didn't close the <html> tag, but it doesn't matter. What is the exact problem? are you experiencing any syntax error? can you briefly explain what you are trying to accomplish? As @Halcyon said, it is quite hard to find the error in all these lines of codes. At least give us some hints or what happens. Commented Jan 19, 2015 at 17:00

2 Answers 2

1

header() has to come before any output, so having it at the bottom will not work. Right now you don't really have an email 'function'. You can wrap that bottom piece of code into a sendEmail function. Then put the call to the function at the end of if ($_SERVER["REQUEST_METHOD"] == "POST") {. You would have to pass all the variables in to the function. Or you could pass $_POST and do you variable cleaning in one function.

Sign up to request clarification or add additional context in comments.

3 Comments

Okay, I did what khartnett proposed. Moved the code back to the top, and ran a request check. So loading the contact.php page now shows the form etc. but once submitted, the page loads to a blank 'contact.php' page. So close....what else do you think I' missing?
You curly braces { } around the bit of email code. They are not needed and might cause some problems
I noticed in your updated code above, you have: ?> then a blank line, than a <?php above your email code. This opens and closes php tags, and the blank line will probably result in a blank line outputted to the browser. Remove both of these and hopefully that will fix it.
0

Move the email part up above the html, where it was redirecting automatically before. You need to add a check to see if there was a post request before sending the email and redirecting. Right after you set $myemail, there is an open bracket. Change this to:

if ($_SERVER["REQUEST_METHOD"] == "POST") {

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.