Send Form data in Email using PHP without refreshing the page - javascript

I have a contact section in my website. I want when a user submit the form all the data should be sent in email plus the page don't reload.
My form:
<label for="name">Name:</label>
<input type="text" class="form-control" name="name" id="name" >
</div>
<div class="form-group">
<label for="email">E-Mail:</label>
<input type="email" class="form-control" name="email" id="email" >
</div>
<div class="form-group">
<label for="mobile">Mobile Number:</label>
<input type="text" class="form-control" name="mobile" id="mobile">
</div>
<div class="form-group">
<label for="message">Message:</label>
<textarea class="form-control" rows="5" name="message" id="message"></textarea>
</div>
<button type="submit" id="send" class="btn btn-primary" style="width: 100%" name="form_submit">Send Message</button>
Javascript
$(document).ready(function(){
$('#send').click(function(){
var name = $('#name').val();
var email = $('#email').val();
var mobile = $('#mobile').val();
var message = $('#message').val();
var varData = 'name=' + name + '&email=' + email + '&mobile=' + mobile + '&message=' + message;
$.ajax({
type: 'POST',
url: 'process.php',
data: varData,
success: function() {
alert("message sent");
}
});
});
});
</script>
Process.php
if (isset( $_POST['form_submit'] ) ) {
// Do processing here.
$name = $_POST['name'];
$email = $_POST['email'];
$mobile = $_POST['mobile'];
$message = $_POST['message'];
if ($name == '' || $email == '' || $mobile=='' || $message=='')
{
echo "<script> alert('please fill the field');
</script>";
exit;
}
elseif ($_POST["email"]<>'') {
$ToEmail = 'email#gmail.com';
$EmailSubject = 'Website Contact form';
$mailheader = "From: ".$_POST["email"]."\r\n";
$mailheader .= "Reply-To: ".$_POST["email"]."\r\n";
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
$MESSAGE_BODY = "Name: ".$_POST["name"]."<br><br>";
$MESSAGE_BODY .= "email: ".$_POST["email"]."<br>";
$MESSAGE_BODY .= "mobile: ".nl2br($_POST["mobile"])."<br>";
$MESSAGE_BODY .= "message: ".nl2br($_POST["message"])."";
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");
echo "<script> alert('mail sent')
</script>";
exit;
} else{
echo "<script> alert ('there is some problem');</script>";
};
}
?>
The problem is the form is not using the code mentioned in "process.php"
How can I ensure the form data is sent in email? I have searched the previous answers but couldn't fix this.

Related

Html and php form not submitting issue

Hi guys I have been having trouble with one of the websites using HTML and PHP
The form doesn't seem to submit or send a message. Attached is the code please any help will be great. Also, the PHP and javascript have been attached for reference.
Also sometimes the page doesn't respond to the button.
If anyone can fix the code will be great.
CONTACT FORM
/*-----------------------------------------------------------------------------------*/
function checkmail(input) {
var pattern1 = /^([A-Za-z0-9_\-\.])+\#([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
if (pattern1.test(input)) {
return true;
} else {
return false;
}
}
function proceed() {
var name = document.getElementById("name");
var email = document.getElementById("email");
var phone = document.getElementById("phone");
var movingfrom = document.getElementById("movingfrom");
var movingto = document.getElementById("movingto");
var date = document.getElementById("date");
var msg = document.getElementById("message");
var errors = "";
if (name.value == "") {
name.className = 'error';
return false;
} else if (email.value == "") {
email.className = 'error';
return false;
} else if (checkmail(email.value) == false) {
alert('Please provide a valid email address.');
return false;
} else if (phone.value == "") {
phone.className = 'error';
return false;
} else if (movingfrom.value == "") {
movingfrom.className = 'error';
return false;
} else if (movingto.value == "") {
movingto.className = 'error';
return false;
} else if (date.value == "") {
date.className = 'error';
return false;
} else if (msg.value == "") {
msg.className = 'error';
return false;
} else {
$.ajax({
type: "POST",
url: "php/submit.php",
data: $("#contact_form").serialize(),
success: function(msg) {
//alert(msg);
if (msg) {
$('#contact_form').fadeOut(1000);
$('#contact_message').fadeIn(1000);
document.getElementById("contact_message");
return true;
}
}
});
}
};
<div class="contact-form">
<!-- Form -->
<div class="margin-top-50">
<div class="contact-form">
<!-- Success Msg -->
<div id="contact_message" class="success-msg"> <i class="fa fa-paper-plane-o"></i>Thank You. Your Message has been Submitted</div>
<!-- FORM -->
<form id="contact_form" class="contact-form" method="post" action="php/submit.php" onsubmit="return ValidateForm()">
<li class="col-sm-6">
<label>
<input type="text" class="form-control" name="name" id="name" placeholder="Your Name">
</label>
</li>
<li class="col-sm-6">
<label>
<input type="text" class="form-control" name="email" id="email" placeholder="E-Mail">
</label>
</li>
<li class="col-sm-6">
<label>
<input type="text" class="form-control" name="phone" id="phone" placeholder="Phone Number">
</label>
</li>
<li class="col-sm-6">
<label>
<input type="text" class="form-control" name="movingfrom" id="movingfrom" placeholder="Moving From">
</label>
</li>
<li class="col-sm-6">
<label>
<input type="text" class="form-control" name="movingto" id="movingto" placeholder="Moving To">
</label>
</li>
<li class="col-sm-6">
<label>
<input type="date" class="form-control" name="date" id="date" placeholder="Date">
</label>
</li>
<li class="col-sm-12">
<label>
<select class="form-control" id="typeofmove" placeholder="Date Of Move" required>
<option>Move Type</option>
<option>Residential Move</option>
<option>Office Move</option>
<option>Inter-City Move</option>
<option>Piano Move</option>
<option>Spa-Pool Move</option>
<option>Pool Table Move</option>
<option>Loading & Unloading Only</option>
<option>Packing</option>
<option>Assembly</option>
<option>TradeMe Pickups</option>
<option>Commercial Delivery</option>
<option>Packaging Material</option>
</select>
</label>
</li>
<li class="col-sm-12">
<label>
<textarea class="form-control" name="message" id="message" rows="5" placeholder="Your Message"></textarea>
</label>
</li>
<li class="col-sm-12">
<button type="submit" value="submit" class="btn" id="btn_submit" onClick="proceed();">Submit</button>
</li>
</ul>
</form>
</div>
</div>
</div>
</div>
<div class="col-md-4 col-sm-5 col-xs-12">
SUBMIT.PHP /*-----------------------------------------------------------------------------------*/
<?php
// specify your email here
$to = 'testmail#gmail.com';
// Assigning data from $_POST array to variables
if (isset($_POST['name'])) { $name = $_POST['name']; }
if (isset($_POST['phone'])) { $name = $_POST['phone']; }
if (isset($_POST['email'])) { $from = $_POST['email']; }
if (isset($_POST['movingfrom'])) { $name = $_POST['movingfrom']; }
if (isset($_POST['movingto'])) { $name = $_POST['movingto']; }
if (isset($_POST['date'])) { $name = $_POST['date']; }
if (isset($_POST['typeofmove'])) { $name = $_POST['typeofmove']; }
if (isset($_POST['message'])) { $message = $_POST['message']; }
// Construct subject of the email
$subject = 'Booking Enquiry ' . $name;
// Construct email body
$body_message .= 'Name: ' . $name . "\r\n";
$body_message .= 'Email: ' . $from . "\r\n";
$body_message .= 'Phone: ' . $phone . "\r\n";
$body_message .= 'Moving From: ' . $movingfrom . "\r\n";
$body_message .= 'Moving To: ' . $movingto . "\r\n";
$body_message .= 'Date Of Move: ' . $date . "\r\n";
$body_message .= 'Message: ' . $message . "\r\n";
// Construct headers of the message
$headers = 'From: ' . $from . "\r\n";
$headers .= 'Reply-To: ' . $from . "\r\n";
$mail_sent = mail($to, $subject, $body_message, $headers);
if ($mail_sent == true){ ?>
<script language="javascript" type="text/javascript">
window.alert("Sent Successfuly.");
</script>
<?php } else { ?>
<script language="javascript" type="text/javascript">
window.alert("Error! Please Try Again Later.");
</script>
<?php
} // End else
?>
First of all you can try check your url.Is it correct or no.For example send anything via ajax and show the response at console log.If it is not working thats mean your url is wrong.

Ajax : one ajax request for another forms

I have 3 contact form in one page (header, page, footer). How i can create one ajax request for all form ? I want when user click each one form send data fron only this form. Just now send from all forms. I used hasClass for determining from which class sending data, but i can't it
HTML:
<form id="contact-form1" method="POST" class="d-flex form">
<input type="text" class="simple-input" id="name" placeholder="Name">
<input type="text" class="simple-input" id="email" placeholder="Email address">
<textarea class="quession-input" id="msg" placeholder="Your question"></textarea>
<div class="checkboks custom-sq">
<input type="checkbox" class="checked-checkbox" name="myCheckboxes[]" id="box1" checked="checked" value="true" />
<label for="box1" class="checkboks-text"><?php echo the_field('checkbox_text', 'option'); ?></label>
</div>
<button type="submit" id="submit" class="danger-btn submit1"><?php echo the_field('btn_send', 'option'); ?></button>
</form>
<form id="contact-form" method="POST" class="d-flex form">
<input type="text" class="simple-input" id="hy_name" placeholder="Name">
<input type="text" class="simple-input" id="hy_email" placeholder="Email address">
<textarea class="quession-input" id="hy_msg" placeholder="Your question"></textarea>
<div class="checkboks custom-sq">
<input type="checkbox" id="box3" class="checked-checkbox" checked="checked" />
<label for="box3" class="checkboks-text"><?php echo the_field('checkbox_text', 'option'); ?></label>
</div>
<button type="submit" id="submit" class="danger-btn hy-submit submit2"><?php echo the_field('btn_send', 'option'); ?></button>
</form>
Ajax:
jQuery('.submit, .hy-submit').on('click', function(e) {
e.preventDefault();
// All data from form
var str = $(this).closest('form').serialize();
// Get current page url
var page_url = window.location.toString();
// Vars
var name = jQuery('#name').val();
var hy_name = jQuery('#hy_name').val();
var email = jQuery('#email').val();
var hy_email = jQuery('#hy_email').val();
var msg = jQuery('#msg').val();
var hy_msg = jQuery('#hy_msg').val();
var subj = jQuery('#subj').val();
var obj=$(this);
if($(obj).hasClass('hy-submit')){
validatehyEmail(hy_email);
if (hy_msg == '' || hy_email == '' || validatehyEmail(jQuery('#hy_email').val()) == false) {
validatehyEmail(hy_email);
validateText(jQuery('#hy_msg'));
validateText(jQuery('#hy_name'));
return false;
}
console.log('submit');
} else if ($(obj).hasClass('submit')) {
validateEmail(email);
if (msg == '' || email == '' || validateEmail(jQuery('#email').val()) == false) {
validateEmail(email);
validateText(jQuery('#msg'));
validateText(jQuery('#name'));
return false;
}
console.log('submit');
}
// Get checkbox value
var chkElem = document.getElementsByName("myCheckboxes[]");
var choice ="";
for(var i=0; i< chkElem.length; i++)
{
if(chkElem[i].checked)
choice = choice + chkElem[i].value;
}
jQuery.ajax({
type: "post",
url: ajaxactionurl,
data: "action=send_email&" + str + "&myCheckboxes=" + choice + "&url=" + page_url,
success: function (response) {
jQuery('#contact-form input').val('');
jQuery('#contact-form textarea').val('');
jQuery('.submit').text('Done!');
},
error: function (jqXHR, textStatus, errorThrown) {
console.log(textStatus);
}
});
});
PHP:
add_action('wp_ajax_nopriv_send_email', 'send_email');
add_action('wp_ajax_send_email', 'send_email');
function send_email() {
$checkbox = $_POST['myCheckboxes'];
if (isset($checkbox)) {
echo $checkbox;
}
$headers = 'Content-Type: text/html; charset="utf-8"';
$name = $_POST['name'];
$url = $_POST['url'];
$hy_name = $_POST['hy_name'];
$from = 'contact#test.com';
$to = 'test#test.com';
$email = $_POST['email'];
$hy_email = $_POST['hy_email'];
$msg = $_POST['msg'];
$hy_msg = $_POST['hy_msg'];
$subject = 'Footer form: ' . $_POST['email'];
$message .= (!empty($name)) ? '<p><strong>User Name</strong> : ' . $name .' </p>' : '';
$message .= (!empty($email)) ? '<p><strong>User Email</strong> : '. $email .'</p>' : '';
$message .= (!empty($msg)) ? '<p><strong>User Message</strong> : '.$msg .'</p>' : '';
$message .= (!empty($checkbox)) ? '<p><strong>Checkboxs</strong> : '.$checkbox .'</p>' : '';
$message .= (!empty($url)) ? '<p><strong>Url:</strong> : '.$url .'</p>' : '';
$message .= (!empty($hy_name)) ? '<p><strong>User Name</strong> : '.$hy_name .'</p>' : '';
$message .= (!empty($hy_email)) ? '<p><strong>User Email</strong> : '.$hy_email .'</p>' : '';
$message .= (!empty($hy_msg)) ? '<p><strong>User Message</strong> : '.$hy_msg .'</p>' : '';
$message .= '</body></html>';
echo mail($to, $subject, $message, $headers);
return $msg;
die();
}
Use form id attribute and use preventDefault method to prevent the forms from submitting.
$('#form-id').preventDefault();

How to prevent spammers to send me blank emails from my contact form

I have simple contact form on my website and few days ago I started to receive half blank emails from contact form on website.
They looking the same as the sample below, filled out just Name and email field, but Phone and Message fields are empty:
Name: 5906f36c9c72b
E-mail: njhjlee#gmail.com (any time different emails)
Phone:
Message:
This is HTML code:
<form id="contactform" action="assets/php/mail_submit.php" method="post">
<div class="row">
<div class="col-xs-12 col-md-4">
<input type="text" name="name" id="name" placeholder="Your Name" required/>
</div><!--column-->
<div class="col-xs-12 col-md-4">
<input type="text" name="email" id="email" placeholder="Your email" required/>
</div><!--column-->
<div class="col-xs-12 col-md-4">
<input type="text" name="phone" id="phone" placeholder="Ваш телефон" required/>
</div><!--column-->
<p class="antispam">Leave this empty: <input type="text" name="url" /></p>
<div class="col-xs-12 col-md-8">
<textarea placeholder="Message" name="message" id="message" required></textarea>
</div><!--column-->
<div class="col-xs-12 col-md-4">
<input class="btn btn-default" id="submit" type="submit" value="Send"/>
</div><!--column-->
</div><!--row-->
</form>
This is PHP script:
<?php
if (!isset($_REQUEST['name']) || !isset($_REQUEST['email']) ||
!isset($_REQUEST['message']) || !isset($_REQUEST['phone'])) {
die();
}
if (isset($_POST['name']) || isset($_POST['email']) ||
isset($_POST['message']) || isset($_POST['phone']) && !empty($_POST['name'])
|| !empty($_POST['email']) || !empty($_POST['message']) |
!empty($_POST['phone'])) {
$your_email="my#mail.com";
$name=$_POST['name'];
$email=$_POST['email'];
$message=$_POST['message'];
$phone=$_POST['phone'];
$to = $your_email;
$subject = "My Website: New Message! \r\n";
$message = '
<html>
<head>
<title>Message from '. $name .'</title>
</head>
<body>
<table class="table">
<tr>
<th align="right">Name:</th>
<td align="left">'. $name .'</td>
</tr>
<tr>
<th align="right">E-mail:</th>
<td align="left">'. $email .'</td>
</tr>
<tr>
<th align="right">Phone:</th>
<td align="left">'. $phone .'</td>
</tr>
<tr>
<th align="right">Message:</th>
<td align="left">'. $message .'</td>
</tr>
</table>
</body>
</html>';
$headers .= "From: no-reply#website.com\r\n";
$headers .= "Reply-To: $email \r\n";
$headers .= "Return-Path: $email\r\n";
$headers .= "X-Mailer: PHP \r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
mail($to, $subject, $message, $headers);
} else {
die();
}
?>
This is AJAX script:
function IsEmail(email) {
var regex = /^([a-zA-Z0-9_\.\-\+])+\#(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]
{2,4})+$/;
return regex.test(email);
}
if($("#contactform").length!=0){
$("#contactform").submit(function (e) {
e.preventDefault();
var name = $("#name").val(),
email = $("#email").val(),
phone = $("#phone").val(),
message = $("#message").val(),
dataString = 'name=' + name + '&email=' + email+ '&phone=' + phone +
'&message=' + message;
if (name === '' || !IsEmail(email) || phone === '' || message ===
'') {
$('#valid-issue').html('Not correct data').slideDown();
} else {
$.ajax({
type: "POST",
url: "/assets/php/mail_submit.php",
data: dataString,
success: function () {
$('#contactform').slideUp();
$('#valid-issue').html('Thank you. Message was sent successfully.').show();
}
});
}
});
}
I've checked logs and found that all spammer bots was from Thor network and with different IPs. I tried captcha but it doesn't help. Help with advise please...
Try to reinforce your javascript checking, also use "trim":
var name = $("#name").val().trim();
Use PHP str_word_count to reinforce the checking for empty message.
eg.
$message= filter_input(INPUT_POST, 'message');
// check $message is not empty and that it contains more than 5 words
if($message != "" || str_word_count($message) < 5) {
echo "Message is valid"
}else{
echo "Invalid Message";
}
I few other wrong things with your PHP code:
You should never access superglobal POST/GET variables directly. Use PHP inbuilt filter functions or a customised one.
$name= filter_input(INPUT_POST, 'name');
Or you can use
function FilterData($value){
$trimmed = trim($value);
$notags = strip_tags($trimmed);
return $notags;
}
Then:
$name = FilterData($_POST['name']);
For the email you can use
$email = filter_var($email, FILTER_SANITIZE_EMAIL);
More info here and here
Hope it helps ;)

Website contact form not receiving messages

Very new html and website developer and unsure why my current contact box is not working. I'm hosting it my own website and when I do attempt to send I get a message sent response but the emails are never received.
I have't eliminated the email hosting being broken by using multiple email addresses to send and receive from but have been unable to get find the source of the problem.
See relevant code bellow
main.js
// Contact form
var form = $('#main-contact-form');
form.submit(function (event) {
event.preventDefault();
var form_status = $('<div class="form_status"></div>');
$.ajax({
url: $(this).attr('action'),
beforeSend: function () {
form.prepend(form_status.html('<p><i class="fa fa-spinner fa-spin"></i> Email is sending...</p>').fadeIn());
}
}).done(function (data) {
form_status.html('<p class="text-success">Thank you for contact us. As early as possible we will contact you</p>').delay(3000).fadeOut();
});
});
sendemail.php
<?php
$name = #trim(stripslashes($_POST['name']));
$from = #trim(stripslashes($_POST['email']));
$subject = #trim(stripslashes($_POST['name']));
$message = #trim(stripslashes($_POST['message']));
$to = '\\myemailaddress//';
$headers = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/plain; charset=iso-8859-1";
$headers[] = "From: {$name} <{$from}>";
$headers[] = "Reply-To: <{$from}>";
$headers[] = "Subject: {$subject}";
$headers[] = "X-Mailer: PHP/".phpversion();
mail($to, $subject, $message, $headers);
die;
index.html
<div class="col-md-8 col-sm-12">
<div class="contact-form bottom">
<h2>Send a message</h2>
<form id="main-contact-form" name="contact-form" method="post" action="sendemail.php">
<div class="form-group">
<input type="text" name="name" class="form-control" required="required" placeholder="Name">
</div>
<div class="form-group">
<input type="email" name="email" class="form-control" required="required" placeholder="Email Id">
</div>
<div class="form-group">
<textarea name="message" id="message" required="required" class="form-control" rows="8" placeholder="Your text here"></textarea>
</div>
<div class="form-group">
<input type="submit" name="submit" class="btn btn-submit" value="Submit">
</div>
</form>
</div>
</div>
Thanks you in advance!
Next issue is you aren't sending any data with your ajax. Using $(formSelector).serialize() simplifies gathering the data
$.ajax({
url: $(this).attr('action'),
//serialize form data and include it with request
data: form.serialize(),
beforeSend: function () {
...
}
}).done(function (data) {
....
});
Currently if you did print_r($_POST); in your php you would see it is empty
After work from #charlietfl answer. Changing the type and url seemed to fix it.
Working code underneath :
main.js:
var form = $('#main-contact-form').serialize();
form.submit(function (event) {
event.preventDefault();
var form_status = $('<div class="form_status"></div>');
$.ajax({
url: $(this).attr('action'),
type: 'POST',
dataType: "json",
beforeSend: function () {
form.prepend(form_status.html('<p><i class="fa fa-spinner fa-spin"></i> Email is sending...</p>').fadeIn());
}
}).done(function (data) {
form_status.html('<p class="text-success">' + data.message + '</p>').delay(3000).fadeOut();
});
});
and my .php
<?php
header('Content-type: ');
$status = array(
'type'=>'success',
'message'=>'Thank you for contact us. As early as possible we will contact you '
);
$name = #trim(stripslashes($_POST['name']));
$email = #trim(stripslashes($_POST['email']));
$subject = #trim(stripslashes($_POST['subject']));
$message = #trim(stripslashes($_POST['message']));
$email_from = $email;
$email_to = '\\myemail//';
$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message;
$success = #mail($email_to, $subject, $body, 'From: <'.$email_from.'>');
echo json_encode($status);
die;

I cannot get my webform to send (php, javascript)

I do not receive any emails when my webform is filled out. It does not give me any errors. Code posted below:
HTML form:
<div class="col-md-6">
<h3>General Questions? <p class="h4">Use our form.</p></h3>
<address>Required fields are marked with an *.</address>
<div class="col-md-12">
<form data-toggle="validator" role="form" class="form-horizontal" id="contactForm">
<!-- Name-->
<div class="form-group">
<label for="inputName" class="control-label"><span class="glyphicon glyphicon-user">
</span> Name * </label><input type="text" class="form-control" id="inputName"
placeholder="What's your name?" data-errors="We need to know what to call you!" required>
<div class="help-block with-errors"></div>
</div>
<!-- Email-->
<div class="form-group">
<label for="inputEmail" class="control-label"><span class="glyphicon glyphicon-
envelope"></span> Email *</label>
<input type="email" class="form-control" id="inputEmail" placeholder="example#example.com"
data-errors="This email address is invalid" required>
<div class="help-block with-errors"></div>
</div>
<!-- Phone Number-->
<div class="form-group">
<label for="inputNumber" class="control-label"><span class="glyphicon glyphicon-phone-
alt"></span> Phone Number</label>
<input type="text" class="form-control" id="inputNnumber" data-minlength="9"
placeholder="(123) 867-5309">
</div>
<!-- Textarea -->
<div class="form-group">
<label for="inputMessage" class="control-label"><span class="glyphicon glyphicon-pencil">
</span> Message *</label>
<textarea rows="8" class="form-control" placeholder="How can we help?" data-errors="We
can't help if you don't tell us how!" required>
</textarea>
<div class="help-block with-errors"></div>
</div>
<!-- Select Basic -->
<div class="control-group text-center">
<span><span class="glyphicon glyphicon-question-sign"></span> <strong>How should we
contact you?</strong></span><br>
<input type="radio" name="contact" value="phone"> by phone
<input type="radio" name="contact" value="email"> by email
</div>
<!-- Button -->
<br>
<div class="form-group">
<button type="submit" class="btn btn-primary btn-sm btn-block">Submit</button>
</div>
</form>
</div>
</div>
Javacript: (it's at devnew.company.com/mail.php and will eventually be at company.com/mail.php. Am I correct in leaving it at /mail.php here?)
var mailUrl = "/mail.php";
var formId = "contactForm";
var completeHTML = ' <div class="alert alert-primary"> Sent! Thanks, we will be in touch
soon. </div> ';
function submitForm(){
$.ajax({
url : mailUrl,
type: 'post',
data:{
inputName : $('#inputName').val(),
inputEmail : $('#inputEmail').val(),
inputNumber : $('#inputNumber').val(),
inputMessage : $('#inputMessage').val(),
contact : $('input[name="contact"]:checked').val()
}
});
}
$(function(){
$('#contactForm').submit(function(){
submitForm();
$(this).html( completeHTML );
return false;
})
})
PHP
<?php
if (isset($_POST['Submit'])) {
if ($_POST['inputName'] != "") {
$_POST['inputName'] = filter_var($_POST['inputName'], FILTER_SANITIZE_STRING);
if ($_POST['inputName'] == "") {
$errors .= 'Please enter a valid name.<br/><br/>';
}
} else {
$errors .= 'Please enter your name.<br/>';
}
if ($_POST['inputEmail'] != "") {
$email = filter_var($_POST['inputEmail'], FILTER_SANITIZE_EMAIL);
if (!filter_var($inputEmail, FILTER_VALIDATE_EMAIL)) {
$errors .= "$inputNumber is not a valid email address.<br/><br/>";
}
} else {
$errors .= 'Please enter your email address.<br/>';
}
if ($_POST['inputNumber'] != "") {
$homepage = filter_var($_POST['inputNumber'], FILTER_SANITIZE_NUMBER_INT);
if ($_POST['inputNumber'] == "") {
$errors .= "Please enter 9 digits.<br/><br/>";
}
} else {
$errors .= 'Please enter your phone number.<br/>';
}
if ($_POST['inputMessage'] != "") {
$_POST['inputMessage'] = filter_var($_POST['inputMessage'],
FILTER_SANITIZE_STRING);
if ($_POST['inputMessage'] == "") {
$errors .= 'Please enter a message to send.<br/>';
}
} else {
$errors .= 'Please enter a message to send.<br/>';
}
if ($_POST['radio'] != "") {
$_POST['radio'] = filter_var($_POST['radio'], FILTER_SANITIZE_STRING);
if ($_POST['inputName'] == "") {
$errors .= 'Please choose an option.<br/><br/>';
}
} else {
$errors .= 'Please choose one.<br/>';
}
if (!$errors) {
$mail_to = 'rnunley#remedypartners.com';
$subject = 'New Mail from Form Submission';
$message = 'From: ' . $_POST['inputName'] . "\n";
$message .= 'Email: ' . $_POST['inputEmail'] . "\n";
$message .= 'Homepage: ' . $_POST['inputNumber'] . "\n";
$message .= "Message:\n" . $_POST['inputMessage'] . "\n\n";
$message .= 'Contact choice: ' . $_POST['radio'] . "\n";
mail($to, $subject, $message);
echo "Thank you for your email, we'll be in touch!<br/><br/>";
} else {
echo '<div style="color: red">' . $errors . '<br/></div>';
}
}
?>
From your code, it doesn't look like you are sending a Submit variable in your ajax data. If that is the case, then your PHP will fail your initial if statement. You could try to add a print_r($_POST); at the beginning of your PHP and then watch the browser console to see what PHP is receiving.
It could be that your javascript thinks that the variables you are trying to pass are being read as non existent javascript vars
below is what maybe could work
$.ajax({
url : mailUrl,
type: 'post',
data:{
"inputName" : $('#inputName').val(),
"inputEmail" : $('#inputEmail').val(),
"inputNumber" : $('#inputNumber').val(),
"inputMessage" : $('#inputMessage').val(),
"Submit" : "TRUE",
"contact" : $('input[name="contact"]:checked').val()
},success: function (data) {
alert(data);
}
});
and add this on the beginning of your mail.php
foreach($_POST as $key => $value){
$content .= $key.' : '.$value;
}
echo $content
Too see what you are recieving on your mailurl

Categories