Hope you can help me. First let me be clear that I have very little php knowledge. I know how to edit php so I can add my information (like an email for a contact form) but aren't really a php coder. My respect goes out to experts like you & I hope you can help me.
I am using a simple php contact form that validates the information entered (to make sure something has been entered into the fields). Up to this point it works perfectly - validating that information has been entered... if not it provides an error message letting you know that you have to enter some sort of information.
The error I am having... is that when all of the fields are entered & you hit the submit button... the page it goes to send the information doesn't load & remains a blank page. I am not sure what the error is being caused from & it only happens when the message is to be sent (no error happens when validating the fields have been entered).
Please see the code below. Thanks.
<?php // Initialize variables to null.
$name =""; // Sender Name
$email =""; // Sender's email ID
$phone =""; // Subject of mail
$message =""; // Sender's Message
$nameError ="";
$emailError ="";
$phoneError ="";
$messageError ="";
$successMessage =""; // On submitting form below function will execute.
if(isset($_POST['submit'])) { // Checking null values in message.
if (empty($_POST["name"])){
$nameError = "Name is required";
}
else
{
$name = test_input($_POST["name"]); // check name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name))
{
$nameError = "Only letters and white space allowed";
}
} // Checking null values in the message.
if (empty($_POST["email"]))
{
$emailError = "Email is required";
}
else
{
$email = test_input($_POST["email"]);
} // Checking null values in message.
if (empty($_POST["phone"]))
{
$phoneError = "Your Phone Number is required";
}
else
{
$phone = test_input($_POST["phone"]);
} // Checking null values in message.
if (empty($_POST["message"]))
{
$messageError = "Message is required";
}
else
{
$message = test_input($_POST["message"]);
} // Checking null values in the message.
if( !($name=='') && !($email=='') && !($phone=='') && !($message=='') )
{ // Checking valid email.
if (preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
{
$header = $name."<". $email .">";
$headers = "ab@abc.com"; /* Let's prepare the message for the e-mail */
$msg = "Hello! $name... Thank You For Contacting us.
Name: $name
E-mail: $email
Phone: $phone
Message: $message
We Will be in contact with You as soon as possible (usually 24 Hours or Less).";
$msg1 = " $name Contacted Us. Here is some information about $name.
Name: $name
E-mail: $email
Phone: $phone
Message: $message "; /* Send the message using mail() function */
if(mail($email, $headers, $msg ) && mail("abc@abc.com", $header, $msg1 ))
{
$successMessage = "Message sent successfully.......";
}
}
else
{ $emailError = "Invalid Email";
}
}
} // Function for filtering input values. function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
All the best, Anton