SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Jul 2007
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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.

    Code:
    $("#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.

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    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".
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    SitePoint Member
    Join Date
    Jul 2007
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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!

  4. #4
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  5. #5
    SitePoint Member
    Join Date
    Jul 2007
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Currently you're code is similar to this:

    Code javascript:
    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

    Code javascript:
    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.
    Last edited by paul_wilkins; Jun 7, 2010 at 04:09. Reason: added dollar sign inside function
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  7. #7
    SitePoint Member
    Join Date
    Jul 2007
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you pmw57, i've got it now!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •