0

I'm not the best at PHP and i'm struggling with what I imagine should be a pretty basic script to send a contact form in an email.

My jquery trigger script is as follows -

data_html = "name="+ names + "&phone_number=" + phone + "&email_address="+ email_address + "&arrival_date=" + arrival + "&nights=" + nights + "&adults=" + adults+ "&children=" + children + "&rooms=" + rooms + "&notes=" + notes;


if (error == false){
$('.error').fadeOut(500);

 $.ajax({
  type: 'POST',
  url: 'sendMail.php',
  data: data_html,
  success: function(msg){

   if (msg == 'sent'){
       alert("sent")
    $('#contact-form').fadeOut();
    $('#success').fadeIn();
    $('#success').html('Your request has been sent, we will respond to you shortly - any issues please call us on 01539 724468')  ;
    $('#name').val(''); $('#phone').val(''); $('#emailAd').val(''); $('#arrival').val(''); $('#notes').val(''); 
   }else{
       alert("not sent")
       $('#contact-form').fadeOut();
       $('#success').fadeIn();
    $('#success').html('Mail Error. Please Try Again.!')  ; 
    setTimeout("$('#success').fadeOut(); $('#contact-form').fadeOut();)",5000)
    ;
   }
  }

});

My php script consists of the following -

<?php

 $name = $_POST['name'];
 $phone = $_POST['phone_number'];
 $email = $_POST['email_address'];
 $arrival = $_POST['arrival_date'];
 $nights = $_POST['nights'];
 $adults = $_POST['adults'];
 $children = $_POST['children'];
 $rooms = $_POST['rooms'];
 $notes = $_POST['notes'];
 $to ='[email protected]';

$message = "";
$message .= "Booking Request from Sundial Website<br>\n<br>\n";
$message .= "*Name: " . htmlspecialchars($name, ENT_QUOTES) . "<br>\n";
$message .= "*Email: " . htmlspecialchars($email, ENT_QUOTES) . "<br>\n";
$message .= "*Phone Number: " . htmlspecialchars($phone, ENT_QUOTES) . "<br>\n";
$message .= "Arrival Date: " . htmlspecialchars($arrival, ENT_QUOTES) . "<br>\n";
$message .= "No of Nights: " . htmlspecialchars($nights, ENT_QUOTES) . "<br>\n";
$message .= "Adults: " . htmlspecialchars($adults, ENT_QUOTES) . "<br>\n";
$message .= "Children: " . htmlspecialchars($children, ENT_QUOTES) . "<br>\n";
$message .= "Rooms: " . htmlspecialchars($rooms, ENT_QUOTES) . "<br>\n";
$message .= "Notes: " . htmlspecialchars($notes, ENT_QUOTES) . "<br>\n";

$headers = "MIME-Version: 1.0\r\nContent-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: \"" . $name . "\" <" . $email . ">\r\n";
$headers .= "Reply-To: " .  $email . "\r\n";
$message = utf8_decode($message);  mail($to, "Booking Request from Sundial Website",       $message, $headers);

 if ($message){
 echo 'sent';
 }else{
  echo 'failed';
 }
 ?>

Can anyone help or offer an alternate method? The files are here if you want to see them live (http://www.offthecuffdesign.co.uk/chilli/) and (http://www.offthecuffdesign.co.uk/chilli/sendMail.php)

7
  • If you are going to check for success and failure of the mail command, you have to get the return value... $message = mail(...); Commented Feb 8, 2012 at 11:56
  • Did you try printing the post values submitted using print_r($_POST), what you are seeing there . Alert(msg) in javascript to print the response . Tell what is your observation . Commented Feb 8, 2012 at 11:59
  • What is the response of ajax request? Commented Feb 8, 2012 at 11:59
  • We're you having a particular problem? I see you have some confusion on character encoding, going back and forth with ISO 8859-1 and UTF8. Commented Feb 8, 2012 at 12:01
  • You've not provided any details of why this does not work as you expect - no email? Error messages? No Ajax call? Something else? Commented Feb 8, 2012 at 12:09

1 Answer 1

1

The last part in your php-script does not make sense...

$message = utf8_decode($message);  mail($to, "Booking Request from Sundial Website",       $message, $headers);
if ($message) {
    echo 'sent';
} else {
    echo 'failed';
}

This should be something like this:

$message = utf8_decode($message);
$result = mail($to, "Booking Request from Sundial Website",       $message, $headers);
if ($result) {
    echo 'sent';
} else {
    echo 'failed';
}

If that is not your problem, please clarify the expected result and what actually happens.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.