-
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)
http://www.webdesignforum.com/images...s/confused.gif Has anyone here ever got ajax within ajax working properly?
-
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.
-
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?
-
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'});
}
});
-
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.
-
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)
-
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 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)
-
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.
-
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.