Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Page 1 of 2 1 2 LastLast
Results 1 to 10 of 14
  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    3
    Member #
    31753
    Ive been building web for quite some time, but I am new to php, jquery and ajax. I am currently building one hell of a complicated site. I have a page that starts with a basic form submitting a image to a page in which a user can extract a hex from the image and submit to a query against the db via an ajax call and show the results or an insert form if it is not in the db, a second ajax call is necessary to submit that form. Ajax is necessary because we do not want the user to have to leave the page.

    I would provide code, but this site relies on several files to function(.js, .html, .css and .php)

    Has anyone here ever got ajax within ajax working properly?

  2.  

  3. #2
    Unpaid WDF Intern TheGAME1264's Avatar
    Join Date
    Dec 2002
    Location
    Not from USA
    Posts
    14,485
    Member #
    425
    Liked
    2783 times
    VERY Pseudocodey:
    Code:
    $.get(page-that-queries-db, ajax-data, processResult);
    page-that-queries-db should be a PHP that outputs an XML file (if you don't know how to create an XML file, you're going to have to learn really fast...sorry, my friend, but that's the answer here).
    Code:
    function processResult() {
        if (there are some results) {
            have-a-div-here-that-outputs-the-results
        } else {
          $.post(your-form-processing-url, ajax-data-related-to-your-form);
        }
    }
    Unfortunately, given your circumstances there's not much more help I can be. But the long and short of it is that you need to use a function from an AJAX get (or post) call to determine if you need to make a second.

    HOWEVER...what I would also suggest is that you try to reduce your AJAX calls to one if you can, just to avoid unnecessary calls to the server.
    If I've helped you out in any way, please pay it forward. My wife and I are walking for Autism Speaks. Please donate, and thanks.

    If someone helped you out, be sure to "Like" their post and/or help them in kind. The "Like" link is on the bottom right of each post, beside the "Share" link.

    My stuff (well, some of it): My bowling alley site | Canadian Postal Code Info (beta)

  4. #3
    Junior Member
    Join Date
    May 2012
    Posts
    3
    Member #
    31753
    I thank you for your timely reply thegame. I never saw the point of outputting a small amount of data to an XML just to have to output it again to my php/HTML page. Is it possible to make an ajax call within an Ajax call?

  5. #4
    WDF Staff smoseley's Avatar
    Join Date
    Mar 2003
    Location
    Boston, MA
    Posts
    9,729
    Member #
    819
    Liked
    205 times
    Sure, you can chain infinite ajax calls if you like... here's an example using jQuery pseudocode:
    Code:
    $.ajax({
        url: '/my/initial-call.php',
        success: function(data, status) {
            $.ajax({url: '/my/on-success-call.php'});
        },
        error: function (data, status) {
            $.ajax({url: '/my/on-error-call.php'});
        }
    });

  6. #5
    Unpaid WDF Intern TheGAME1264's Avatar
    Join Date
    Dec 2002
    Location
    Not from USA
    Posts
    14,485
    Member #
    425
    Liked
    2783 times
    What he said.

    And the point (in this case) is to create a PHP file that can process your initial call and output XML to let your Javascript know that something happened. In your case, you want it to indicate the result of the query against the DB. A PHP file is not exclusively limited to HTML/JS/CSS on the client side...that's the key here.

    I'm not a PHP guy, so I don't know if this would work, but this would give you the general idea.
    If I've helped you out in any way, please pay it forward. My wife and I are walking for Autism Speaks. Please donate, and thanks.

    If someone helped you out, be sure to "Like" their post and/or help them in kind. The "Like" link is on the bottom right of each post, beside the "Share" link.

    My stuff (well, some of it): My bowling alley site | Canadian Postal Code Info (beta)

  7. #6
    WDF Staff smoseley's Avatar
    Join Date
    Mar 2003
    Location
    Boston, MA
    Posts
    9,729
    Member #
    819
    Liked
    205 times
    Better to output a JSON response to an AJAX call. JS parses JSON natively, so it's significantly faster (also, smaller data footprint means faster transfer from server too)

  8. #7
    Unpaid WDF Intern TheGAME1264's Avatar
    Join Date
    Dec 2002
    Location
    Not from USA
    Posts
    14,485
    Member #
    425
    Liked
    2783 times
    I'd agree with that, except that I've never used JSON before and don't know all that much about it (looks weird to me.)
    If I've helped you out in any way, please pay it forward. My wife and I are walking for Autism Speaks. Please donate, and thanks.

    If someone helped you out, be sure to "Like" their post and/or help them in kind. The "Like" link is on the bottom right of each post, beside the "Share" link.

    My stuff (well, some of it): My bowling alley site | Canadian Postal Code Info (beta)

  9. #8
    WDF Staff smoseley's Avatar
    Join Date
    Mar 2003
    Location
    Boston, MA
    Posts
    9,729
    Member #
    819
    Liked
    205 times
    If you use Javascript at all, JSON shouldn't look weird. It's essentially a Javascript Object Literal, with the exception that the values can only be of types null, string, int, double, bool, object, or array. In other words, key-value pairs.

    I used Object Literals in my example above (encased in braces {}). You'll notice, however, that 2 of the values are anonymous functions (JSON doesn't allow functions as values)

  10. #9
    Unpaid WDF Intern TheGAME1264's Avatar
    Join Date
    Dec 2002
    Location
    Not from USA
    Posts
    14,485
    Member #
    425
    Liked
    2783 times
    I know it shouldn't...it just does. :P I don't know why it does, but it does. I know how to use XML, so therefore I stick to what I know.
    If I've helped you out in any way, please pay it forward. My wife and I are walking for Autism Speaks. Please donate, and thanks.

    If someone helped you out, be sure to "Like" their post and/or help them in kind. The "Like" link is on the bottom right of each post, beside the "Share" link.

    My stuff (well, some of it): My bowling alley site | Canadian Postal Code Info (beta)

  11. #10
    Junior Member
    Join Date
    May 2012
    Posts
    3
    Member #
    31753
    Thanx for your input guys. I got it working. I had a type-o in one of my name value pairs in the ajax call. I plan to do some reading on xml/json with ajax. I have worked with xml at a moderate level before, but for this site xml is just an unnecessary step in the flow of data.

    In case anyone was wondering.
    The first page uploads an image(to the database aswell as to a <canvas>). I used a jquery plugin to let the user grab a colour from the image in either hex or rgb. then ajax/php queries the db for that colour. if the colour is not there a form appears to let an user name and add that colour. the second ajax call is to a php emailer page that sends the n/v pairs to an admins email for approval or rejection. the approval will send the data to a php page on the site to add it to the database. rejection will trigger a php email back to the original user.
    I'm happy to share any of my work with anyone, incase anyone wants to use it on their site.
    thanx for your replies guys.


Page 1 of 2 1 2 LastLast

Remove Ads

Posting Permissions

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

Search tags for this page

ajax within ajax

Click on a term to search for related topics.
All times are GMT -6. The time now is 01:46 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com