CJS
CJS

Reputation: 351

Running AJAX query to refresh div with php file

I'm currently working on a multi-step query form which can be found at: http://jsfiddle.net/xSkgH/47/.

I'm trying to submit the variables via jQuery AJAX (process.php will handle the processing) and refresh the div last-step with the div in the process.php called result. How can I achieve this?

I've so far managed to accomplish this using the jQuery form plugin by malsup (http://jquery.malsup.com/form/) and now need to using the jQuery AJAX method to accomplish it as part of a strict specification.

This is the code I had been using (with the jQuery form plugin):

// prepare the form when the DOM is ready 

$(document).ready(function() { 
var options = { 
    target:        '#result',  
    beforeSubmit:  showRequest,  
    success:       showResponse   
}; 

// bind to the form's submit event 

$('#task5_booking').submit(function() { 
    $(this).ajaxSubmit(options); 
    return false; 
    }); 
});  

// pre-submit callback 

function showRequest(formData, jqForm, options) { 
    var queryString = $.param(formData); 
    // alert('About to submit: \n\n' + queryString); 
}

// post-submit callback 

function showResponse(responseText, statusText, xhr, $form)  {
    $('#last-step').fadeOut(300, function() {
    $('#result').html(responseText).fadeIn(300);
    });
}

Many thanks!

Upvotes: 2

Views: 1058

Answers (2)

Ropstah
Ropstah

Reputation: 17794

Use jQuery.ajax to handle the last step:

http://api.jquery.com/jQuery.ajax/

   else if (whichStep == 'last-step') {
        $.ajax( {
            url:'urltophp.php',
            data: {}, // your data
            dataType: 'json', //your datatype
            type: 'POST',   //or GET
            success: function(r) {
                //your callback here...
            }
        });
    }

Edit:

$('#task5_booking').submit(function(e) { 
    $(e).preventDefault();  //prevent the default form submit()

    var formData = $(this).serialize(); //serialize the form fields data...


    $.ajax( {
        url:'urltophp.php',
        data: formData, // your data
        dataType: 'json', //your datatype
        type: 'POST',   //or GET
        success: showResponse
    });

    //$(this).ajaxSubmit(options); 
    //return false; 
    //}); 
});

Change this:

function showResponse(responseText, statusText, xhr, $form)  {
    $('#last-step').fadeOut(300, function() {
    $('#result').html(responseText).fadeIn(300);
    });
}

To this:

function showResponse(responseText)  {
    $('#last-step').fadeOut(300, function() {
        $('#result').html(responseText).fadeIn(300);
    });
}

Upvotes: 2

Rodik
Rodik

Reputation: 4092

use http://api.jquery.com/load/ . it's like using .ajax, only easier and fits your requirements.

$('#last-step').load(url, data, function(){}) sends a post request, and fills the html content of 'last-step' with whatever the url printed out into the response html.

Upvotes: 1

Related Questions