I have a contact us page in a website made for a client, which I am hosting from a sub-directory of my main website. When someone sends a message from "contact us" page, its sent to the website owner's email, but the values are empty. But when I tested the same two pages from the root directory they worked fine.
Following is the code.
HTML PAGE
<form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="sendemail.php" role="form">
<div class="row">
<div class="col-sm-5">
<div class="form-group">
<input name="fname" type="text" class="form-control" required="required" placeholder="First Name">
</div>
<div class="form-group">
<input name="lname" type="text" class="form-control" required="required" placeholder="Last Name">
</div>
<div class="form-group">
<input name="email" type="text" class="form-control" required="required" placeholder="Email address">
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary btn-lg">Send Message</button>
</div>
</div>
<div class="col-sm-7">
<textarea name="message" id="message" required="required" class="form-control" rows="8" placeholder="Message"></textarea>
</div>
</div>
</form>
PHP FILE
<?php
header('Content-type: application/json');
$status = array(
'type'=>'success',
'message'=>'Email sent!'
);
$fname= @trim(stripslashes($_POST['fname']));
$lname = @trim(stripslashes($_POST['lname']));
$name = $fname.' '.$lname;
$email = @trim(stripslashes($_POST['email']));
$message = @trim(stripslashes($_POST['message']));
$email_from = $email;
$email_to = 'my.email@gmail.com';
$body = 'Name: ' . $name . "
" . 'Email: ' . $email . "
" . 'Message: ' . $message;
$success = @mail($email_to, 'An email from website\'s form', $body, 'From: <'.$email_from.'>');
echo json_encode($status);
die;
When I put both of these files in the root (public_html) through CPanel's file manager, they work fine, but if I put them in the sub-folder than the values are empty.
Following are the examples.
Example 1, when sent from the sub-folder
Example 2, when sent form the main directory
check your file "main.js" and replace:
$.post($(this).attr(‘action’), function(data) {
for
$.post($(this).attr('action'), $('.contact-form').serialize(), function(data) {
the complete "main.js" is:
jQuery(function($) {
//Ajax contact
var form = $('.contact-form');
form.submit(function () {
$this = $(this);
$.post($(this).attr('action'), $('.contact-form').serialize(), function(data) {
$this.prev().text(data.message).fadeIn().delay(3000).fadeOut();
},'json');
return false;
});
//Goto Top
$('.gototop').click(function(event) {
event.preventDefault();
$('html, body').animate({
scrollTop: $("body").offset().top
}, 500);
});
//End goto top
});