tags:

views:

41

answers:

1

I'm using jQuery's AJAX function to send a message from a contact form -

$('form button').click(function() {

$("input").removeClass("error");
$("textarea").removeClass("error");

var name = $("#name").val();
if (name == "" || name == "Name" || name == "Namn")
{
$("#name").addClass("error");
$("#name").focus();
return false;
}

var email = $("#email").val();
if (email == "" || email == "Email" || email == "Epost")
{
$("#email").addClass('error');
$("#email").focus();
return false;
}

var message = $("#message").val();
if (message == "")
{
$("#message").addClass('error');
$("#message").focus();
return false;
}

// Non-verifying fields
var phone = $("input#phone").val();

// Gather data
var post = 'name='+ name + '&email=' + email + '&phone=' + phone +'&message=' + message;

// Disable form
var limit = document.forms[0].elements.length;
for (i=0; i<limit; i++) {
document.forms[0].elements[i].disabled = true;
}

// Send data
$.ajax({
type: "POST",
url: "form_handler.php",
data: post,
success: function()
{ $('div.contact form').animate({opacity: 0.25}, function() { $('div.contact div.confirm').fadeIn(200) ;}); },
error: function()
{ $('div.contact form').animate({opacity: 0.25}, function() { $('div.contact div.deny').fadeIn(200) ;}); }
});
return false;
});

I know this is not the safest method considering I reveal the Mail file in my JS code but nevertheless I want this to work before I decide to try anything else. In my contact form I have the above fields (name, email, phone and message) and in "form_handler.php" the settings look like this -

<?php
header('Content-type: text/html; charset=UTF-8');

$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];

$to = "[email protected]";
$subject = "Meddelande från x.se";
$body = "------------------------------------------------------------------------------------------------------------------------\n\n";
$body .= "Meddelande från $name:\n\n";
$body .= "$message\n\n";
$body .= "Avsändarens epost: $email\n";
$body .= "Avsändarens telefonnummer: $phone\n\n";
$body .= "------------------------------------------------------------------------------------------------------------------------";
$headers = "From: $email";

mail($to,$subject,$body,$headers);
?>

When I combine the scripts I manage to generate a message though it doesn't contain any form data. Have I missed out on something?

+1  A: 

I think you'll actually want to pass a JSON array as the data parameter instead of the GET-style string.

Something like:

post = { 'name' : name, ... }
infamouse
I'm confused :)
Staffan Estberg
If you mean switching to "var post = { 'name' : name, 'email' : email, 'phone' : phone, 'message' : message };" in the JS file it didn't help.
Staffan Estberg
Then get Firebug open and look at the AJAX request. The net tab has a really easy way to see what is being sent to your server. If the data is getting to the server then figure out what's wrong with your PHP.
infamouse
Not sure what to look for in the Net tab but I can't see any of the field values.Think I'm going for another solution, this is just frustrating.
Staffan Estberg
I believe he meant the console tab, which displays detailed information about every ajax request, when enabled.
Maerlyn
In the Net tab you can see the outgoing request, a list of parameters sent, the headers, and the response. What more could you possibly need? If you put a var_dump of $_POST in the PHP file and examine the response in the Net tab you should get a complete picture here...
infamouse
I think I know what you're refering to now. There's nothing displaying in the "Response" tab under the process for form_handler.php. I changed some variable names just to see how errors display.
Staffan Estberg
Wow. Just wow. You know, I switched back to Firefox today because Chrome kept bugging and I got lack of it (even though I favor the browser because it's slim and doesn't require much). So what happens - this form thing that I've been working on the entire evening proves to be a Firefox THING. It works perfect in Chrome and Safari... Am I frustrated? Somewhat. Am I confused? Yes. Why on Earth does it not work in Firefox? I mean, parts of the JavaScript do work (such as fetching the form_handler.php through ajax).
Staffan Estberg
Can't debug from your computer but it could be the Content-type header your sending back is incorrect. Frankly, you're not sending back any HTML so you probably don't need it anyways.
infamouse