Jquery.load alternative to insert returned values in different places in html

Hi,

I’m currently using this bit of code which submits some form values to a php script which then returns the results into the target html as specified.

$("#ajax_totals").load("shop/calculate_order_cost", $("#shop_form").serializeArray());

However what i’d like to do is submit the form items using serializeArray but then return multiple items from the php script and insert them in multiple places on the page, not just in the form itself.

I know i could simply use that line of code 4 times over with 4 different html targets but i don’t feel it is very efficient or DRY. So i’d like to know the best way to submit multiple values to a php script, return multiple responses and use those responses in multiple places in the html.

I hope that makes sense!

Thank you very much in advance for any help anyone can provide.

Add a callback and parse the response. You only need one request and one response.
$.load( url, data, function ( response, status, xhr ) { // code... } );
You probably want “get” and not “load”.

I can only use post in my app, i cannot use get url’s with the system i’m using unfortunately.

Could you expand a bit on how this would work please? I must admit i’m struggling with it a bit.

Thanks!

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

Thanks, to be honest, i’ve had a good read of that and haven’t been able to connect the dots in my head on how i can use that to do what i want. I’m very much a js noob as you can see.

Not to worry, i’ll have another try.

Currently you’re code is similar to this:


var url = "shop/calculate_order_cost";
var sendData = $("#shop_form").serializeArray();
$("#ajax_totals").load(url, sendData);

Your advice is to use post instead, where you can achieve what you’re currently doing with


var url = "shop/calculate_order_cost";
var sendData = $("#shop_form").serializeArray();
$.post(url, sendData, function (successData) {
    $("#ajax_totals").html(successData);
});

It is inside that function that you would break-up the data from the server and place it in different locations.

Thank you pmw57, i’ve got it now!