...

View Full Version : Absolute form confirmation (jQuery + PHP)



q1h
05-24-2011, 09:07 PM
I have a simple php mailer:


<?php
$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$message = $_REQUEST['message'];
$to = "whoever@me.com";
$subject = "Some subject";
$header = "From: $name <$email>";
if($name == "" || $email == ""){
echo "Some reminder to fill in all fields.";
}else{
mail($to, $subject, "$message", $header);
echo "Some success message.";
}
?>

My AJAX jQuery form looks like this:


$(document).ready(function () {
var name=encodeURIComponent(document.getElementById("name").value);
var email=encodeURIComponent(document.getElementById("email").value);
var message=encodeURIComponent(document.getElementById("message").value);
$("#form").submit(function (event) {
event.preventDefault();
$.ajax({
type: "post",
dataType: "html",
url: 'mail.php',
data: "name="+name+"&email="+email+"&message="+message,
success:function(something){}
});
});
});

Basically, I want the PHP message to return to the page via the success function, but I'm not sure how to make that happen. this code [document.getElementById("results").innerHTML=something.responseText;] returns an "undefined" error. The reason I want this is because most jQuery forms respond with success if the the form has been submitted successfully (i.e., all fields have been correctly filled in), but this does not mean that the PHP has processed successfully. Any help would be appreciated.

DanInMa
05-24-2011, 09:52 PM
you doing things the hard way.

- here's some info on error handling as well - http://www.maheshchari.com/jquery-ajax-error-handling/


$(document).ready(function () {
var datastring= $("#form").serialize();
$("#form").submit(function (event) {
event.preventDefault();
$.ajax({
type: "post",
dataType: "html",
url: 'mail.php',
data: datastring,
success:function(html){
$("#results").html(html);
}
});
});
});

q1h
05-25-2011, 03:02 AM
Thanks for your reply. I tried using .serialize, but it broke my PHP somehow. And using success:function(html){ $("#results").html(html); } isn't calling the PHP output. Any suggestions? Thanks.

DanInMa
05-25-2011, 01:59 PM
Thanks for your reply. I tried using .serialize, but it broke my PHP somehow. And using success:function(html){ $("#results").html(html); } isn't calling the PHP output. Any suggestions? Thanks.


- Please define how it broke your php, because serialize (http://api.jquery.com/serialize/) does the same thing your doing with the longer version of your code -

- the results only return on success, so perhaps it's failing. I've always found the php is kind of stingy on providing error messages, make sure you put in some type of echo on your php page that will fire some info on success or fail, maybe that will help.

also Id put in a temp alert or console.log to see how the datastring is actually coming out

q1h
06-09-2011, 09:38 PM
Sorry, it actually does work. My datastring had a capital "S", which firebug helped my find. Thanks for your help.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum