Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Post varible from PHP to awating AJAX jQuery?

  • This topic is empty.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #25443
    bor003
    Member

    Hello,
    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 :)
    HTML

    Code:









    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 :)

    #60724

    This 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’,
    … snip
    Code:
    echo(json_encode(array(
    ‘allDone’ => TRUE,
    ‘response’ => $response
    )));

    http://docs.jquery.com/Specifying_the_D … X_Requests

    #62151
    bor003
    Member

    Just got back after my month and was very pleased to get this reply!

    Thanks so much for your help :)!

Viewing 3 posts - 1 through 3 (of 3 total)
  • The forum ‘Back End’ is closed to new topics and replies.