Smurfy Smurfy - 6 months ago 35
PHP Question

Contact Form Not Working and coming up with no direct access

I feel as if everything is correctly done but when submitting the form it comes up no direct access. This is from a template website that didnt have a contact form so i found one online but it doesnt seem to be working with it.

here is the html

<form action="mail.php" method="post" class="wow fadeInUp" data-wow-delay="0.6s">
<div class="col-md-6 col-sm-6">
<input type="text" name="name" class="form-control" placeholder="Your Name...">
</div>
<div class="col-md-6 col-sm-6">
<input type="email" name="email" class="form-control" placeholder="Your Email...">
</div>
<div class="col-md-12 col-sm-12">
<textarea rows="6" name="message" class="form-control" placeholder="Your Message" required="">
</textarea>
</div>
<div class="col-md-offset-4 col-md-8 col-sm-offset-4 col-sm-8">
<input type="submit" class="form-control" value="SEND">
</div>
</form>


Here is the php

<?php
/**
* sends mail submitted from the contact form
*/

/*
EDIT BELOW
*/
$to_Email = "appydevelopers@gmail.com"; //Replace with your email address
$subject = 'Appydeveloper Site'; //Subject line for emails
/*
EDIT ABOVE
*/


if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest')
die("No direct access.");

if(!isset($_POST["name"]) || !isset($_POST["email"]) || !isset($_POST["message"])) {
$output = json_encode(array('type'=>'error', 'text' => 'Input fields are empty!'));
die($output);
}

//additional validation
$user_Name = filter_var($_POST["name"], FILTER_SANITIZE_STRING);
$user_Email = filter_var($_POST["email"], FILTER_SANITIZE_EMAIL);
$user_Message = filter_var($_POST["message"], FILTER_SANITIZE_STRING);

if(strlen($user_Name)<4) {
$output = json_encode(array('type'=>'error', 'text' => 'Name is too short or empty!'));
die($output);
}
if(!filter_var($user_Email, FILTER_VALIDATE_EMAIL)) {
$output = json_encode(array('type'=>'error', 'text' => 'Please enter a valid email!'));
die($output);
}
if(strlen($user_Message)<5) {
$output = json_encode(array('type'=>'error', 'text' => 'Too short message! Please enter something.'));
die($output);
}

$sentMail = @mail($to_Email, $subject, $user_Message .' -'.$user_Name, $headers);

if(!$sentMail) {
$output = json_encode(array('type'=>'error', 'text' => 'Server error, could not send email. Sorry for the inconvenience.'));
die($output);
} else {
$output = json_encode(array('type'=>'success', 'text' => 'Message successfully sent!'));
die($output);
}
?>

Answer

When you are not calling this php file through Ajax why are you checking it here isset($_SERVER['HTTP_X_REQUESTED_WITH']) This is used when you are calling through Ajax. So it is not set at all.

comment this line and check

//if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest')         //die("No direct access.");
Comments