hey everyone, hoping someone can help with this issues, been trying to figure it out for a few days now.
doing a basic contact page with processing in the same page and having issues with php processing script. Page throws a white page and after some searching found its a 500 server error. Server has been checked every other page works like it should. When i remove the form processing script no more server error which leads me to something in script is the cause.
<?php
// Define variables and set to empty values
$result = $name = $email = $phone = $message = $human = "";
$errName = $errEmail = $errPhone = $errMessage = $errHuman = "";
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$human = intval($_POST['human']);
$from = 'system@1232.com';
$to = 'test@mydomain.com';
$subject = 'Message From Contact Page';
$headers = "From:$from\r\nReply-to:$email";
$body = "From: $name\n E-Mail: $email\n Phone: $phone\n Message: $message";
}
// Check if name is entered
if (empty($_POST["name"])) {
$errName = "Please enter your name.";
}
//empty($_POST["name"])
else {
$name = test_input($_POST["name"]);
}
if (empty($_POST["phone"])) {
$errName = "Please enter your phone number.";
}
//empty($_POST["phone"])
else {
$name = test_input($_POST["name"]);
}
// Check if email is entered
if (empty($_POST["email"])) {
$errEmail = "Please enter your email address.";
}
//empty($_POST["email"])
else {
$email = test_input($_POST["email"]);
// check if e-mail address is valid format
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errEmail = "Invalid email format.";
} //!filter_var($email, FILTER_VALIDATE_EMAIL)
}
//Check if message is entered
if (empty($_POST["message"])) {
$errMessage = "Please enter your message.";
}
//empty($_POST["message"])
else {
$message = test_input($_POST["message"]);
}
//Check if simple anti-bot test is entered
if (empty($_POST["human"])) {
$errHuman = "Please enter the sum.";
}
//empty($_POST["human"])
else {
if ($human !== 12) {
$errHuman = 'Wrong answer. Please try again.';
} //$human !== 12
}
//sanitize data inputs
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
$data = (filter_var($data, FILTER_SANITIZE_STRING));
return $data;
}
// If there are no errors, send the email & output results to the form
if (!$errName && !$errEmail && !$errPhone && !$errMessage && !$errHuman) {
if (mail($to, $subject, $body, $from)) {
$result = '<div class="alert alert-success"><h2><span class="glyphicon glyphicon-ok"></span> Message sent!</h2><h3>Thank you for contacting us. Someone will be in touch within in 1 business day.</h3></div>';
} //mail($to, $subject, $body, $from)
else {
$result = '<div class="alert alert-danger"><h2><span class="glyphicon glyphicon-warning-sign"></span> Sorry there was a form processing error.</h2> <h3>Please try again later.</h3></div>';
}
}
//sanitize data inputs
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
$data = (filter_var($data, FILTER_SANITIZE_STRING));
return $data;
}
//end form processing script
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Contact US</title>
......
And the form:
<!-- Contact Us -->
<div class="contact-us-container">
<div class="container">
<div class="row">
<div class="col-sm-7 contact-form wow fadeInLeft">
<p>
<!--when submit button is clicked, show results here-->
<div class="form-group">
<div class="col-sm-10"> <?php echo $result;?> </div>
</div>
</p>
<form role="form" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
<div class="form-group">
<label for="contact-name">Name</label>
<span class="required">*</span>
<input type="text" name="name" placeholder="Enter your name..." class="contact-name" id="contact-name" value="<?php echo $name;?>">
<span class="required small"><?php echo $errName;?></span>
</div>
<div class="form-group">
<label for="contact-email">Email</label>
<span class="required">*</span>
<input type="text" name="email" placeholder="Enter your email..." class="contact-email" id="contact-email" value="<?php echo $email;?>">
<span class="required small"><?php echo $errEmail;?></span>
</div>
<div class="form-group">
<label for="contact-phone">Phone</label>
<span class="required">*</span>
<input type="text" name="phone" placeholder="Enter your phone..." class="contact-phone" id="contact-phone" value="<?php echo $phone;?>">
<span class="required small"><?php echo $errPhone;?></span>
</div>
<div class="form-group">
<label for="contact-subject">Subject</label>
<span class="required">*</span>
<input type="text" name="subject" placeholder="Your subject..." class="contact-subject" id="contact-subject" value="<?php echo $subject;?>">
<span class="required small"><?php echo $errSubject;?></span>
</div>
<div class="form-group">
<label for="contact-message">Message</label>
<span class="required">*</span>
<textarea name="message" placeholder="Your message..." class="contact-message" id="contact-message" value="<?php echo $message;?>"></textarea>
<span class="required small"><?php echo $errMessage;?></span>
</div>
<div class= "form-group">
<label for="human">Human Test</label>
<span class="required">*</span>
<p>6 + 6 = ?</p>
<input type="text" class="contact-human" id="human" name="human" placeholder="Your Answer..." value="<?php echo $human;?>">
<span class="required small"><?php echo $errHuman;?></span>
</div>
<button type="submit" class="btn">Send</button>
</form>
</div>