- This topic is empty.
-
AuthorPosts
-
July 13, 2009 at 2:18 pm #25443
bor003
MemberHello,
At the moment I have a contact form (will post all the code below), I use the AJAX function in jQuery to post all the information over to the PHP file but then I would like it to post back varibles not HTML. The reason for this is that I would like a var allDone which will = false but if it is all good then I know which animation path to take. IE fade back in and flash or just fade out and say thanks!
Can this be done?Here is my code :)
HTMLCode:
JQUERY
Code:$(function(){
$(‘input#submit’).click(function(){
$(‘.loader’).fadeIn(“slow”);
$(‘input#submit’).fadeOut(“fast”);
$(‘#theyFill’).animate({opacity: 0.3}, 500);var name = $(‘input#name’).val();
var email = $(‘input#email’).val();
var subject = $(‘input#subject’).val();
var message = $(‘textarea#message’).val();var allDone = false;
$.ajax({
type: ‘post’,
url: ‘theSender.php’,
data: ‘name=’ + name + ‘&email=’ + email + ‘&subject=’ + subject + ‘&message=’ + message + ‘&allDone’ + allDone,success: function(results){
$(‘.loader’).fadeOut(‘slow’);
$(‘input#submit’).animate({opacity: 1.0}, 3000).fadeIn(“slow”);if(allDone == true){
$(‘#results’).html(“allDone is now true”);
}else{$(‘#results’).html(“allDone is still false”);
}
//$(‘#results’).html(results);}
});// end of ajax
});
});And the PHP
Code:“);
echo “var allDone = true”;
}
else{
$response = (isset($error[‘name’])) ? “” . $error[‘name’] . “n” : null;
$response .= (isset($error[’email’])) ? “” . $error[’email’] . “n” : null;
$response .= (isset($error[‘subject’])) ? “” . $error[‘subject’] . “” : null;
$response .= (isset($error[‘message’])) ? “” . $error[‘message’] . “n” : null;
echo $response;
}
?>Thanks for any help :)
July 15, 2009 at 8:25 am #60724davesgonebananas
MemberThis is easily enough achieved. You simple specify a dataType option of "json" in your jQuery ajax call and have your php script return a json object instead of an html fragment.
Code:$.ajax({
type: ‘post’,
url: ‘theSender.php’,
dataType: ‘json’,
… snipCode:echo(json_encode(array(
‘allDone’ => TRUE,
‘response’ => $response
)));August 12, 2009 at 2:28 pm #62151bor003
MemberJust got back after my month and was very pleased to get this reply!
Thanks so much for your help :)!
-
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.