[member=32030]lothop[/member] - I tried to implement the code you recommended. Now the form is stopping and reporting errors for the user upon invalid/empty inputs, but now its stopping even if the fields are valid and proper. Upon submit, the page just reloads with clear fields and doesn’t send email or redirect. So this means a) I just have something else stopping it because of my code. Or b) this isn’t the method that’s going to work, this whole php script is just bad.
I may just have to put it aside at this point, remove the contact form completely. I have to get the site live asap and just don’t know enough about server side php. Here is the latest code if anyone wants to chime in on what else could be the issue. In the meantime, I shall move onto completing the html.
Thanks for your help guys, I didn’t think it would be this difficult.
[php]<?php
// define variables and set to empty values
$company=""; $fname=""; $lname=""; $email=""; $phone=""; $address=""; $city=""; $provincestate=""; $country=""; $location=""; $size=""; $type=""; $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;
}
$companyErr=""; $fnameErr=""; $lnameErr=""; $emailErr=""; $phoneErr=""; $addressErr=""; $cityErr=""; $provincestateErr=""; $countryErr=""; $locationErr=""; $sizeErr=""; $typeErr=""; $messageErr="";
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”;
$error = 1;
} 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”;
$error = 1;
}
}
if (empty($_POST[“last-name”])) {
$lnameErr = “Last name is required”;
$error = 1;
} 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”;
$error = 1;
}
}
if (empty($_POST[“email”])) {
$emailErr = “Email is required”;
$error = 1;
} 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”;
$error = 1;
}
}
if (empty($_POST[“phone”])) {
$phoneErr = “Phone number is required”;
$error = 1;
} 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”;
$error = 1;
}
}
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”;
$error = 1;
} else {
$location = test_input($_POST[“location”]);
// check if location only contains letters
if (!preg_match("/^[a-zA-Z ]*$/",$location)) {
$locationErr = “Please enter a city”;
$error = 1;
}
}
if (empty($_POST[“size”])) {
$sizeErr = “Please enter a number”;
$error = 1;
} else {
$size = test_input($_POST[“size”]);
}
if (empty($_POST[“type”])) {
$typeErr = “Please select 1”;
$error = 1;
} else {
$type = test_input($_POST[“type”]);
}
if (empty($_POST[“message”])) {
$message = “”;
} else {
$message = test_input($_POST[“message”]);
}
}
$myemail = ‘xxxxxxxxxxxxxxxxx’;//<-----Put Your email address here.
if ($server[“REQUEST_METHOD”] == “post” && !isset($error)){
$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: $email\n";
$headers .= "Reply-To: $email";
mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
header(‘Location: thankyou.html’);
exit();
}
?>[/php]
HTML
[code]<form id=“contact-form” method=“post” action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Contact Information
Company Name
First Name * <?php echo $fnameErr;?>
Last Name * <?php echo $lnameErr;?>
Email * <?php echo $emailErr;?>
Phone * <?php echo $phoneErr;?>
City
Province/State
Country
Project Information
Location
* <?php echo $locationErr;?>
System Size * <?php echo $sizeErr;?>
PV Type:
* <?php echo $typeErr;?>
Residential
Commercial
Agricultural
Net-Metering
Off-Grid
[/code]