views:

467

answers:

5

Okay, a little preface: I am still a beginner when it comes to AJAX, and dynamic web stuff. My problem is similar to what was asked in the following, but I think that discussion is using a framework, and I am not.

Potentially related post

Here is my scenario:

I have several select elements in a form. The initial select's option elements are populated each time the page loads. The option elements for the subsequent selects get populated depending on what the user selects in the previous select. So it is like a chain. The options in the selects are populated by making AJAX function calls (just my own javascript) that in turn call a php file to get values from a database and build a responseText to fill each subsequent select based on the selection made in the previous select. Hopefully that is clear? All that works fine and dandy.

My problem starts here:

When the user submits the form, a php file is run to process the data, displaying success or any issues, and then I return the user to the form page. I want to reselect all the options they had selected prior to submitting, since some of this is a repetative task and this would save them time. My first step is to recall the required AJAX functions to repopulate the select elements, and then I thought I could run javascript in the form page to select the previously selected options. However, when I try to run javascript on the form page to select the options, it is running before the AJAX calls finish. Therefore, because the options are populated by the AJAX calls, the options do not exist yet in the selects, so I cannot select them. I tried writing some test code to insert a new option to see when the code runs, and sure enough, that new option gets added to the select before my AJAX populated ones go in. In order to track if options were selected, I am passing back the selection option values through the URL, and then processing the $_GET array in my form page.

So my question boils down to:

Is there something I could do that would prevent my javascript that selects the selected options from running until the AJAX populating functions finish?

I would also accept responses like "Your whole approach is bogus! Where did you get your AJAX coding license??!! A Cracker Jack box??" Although, just a few of those responses please, I'm a fragile flower ;)

Thanks in advance, Carvell Fenton

P.S. Hopefully that's not too much preamble, but I thought the background was necessary.

+1  A: 

It would help if you show us your code.

Are you making the AJAX call synchronously? That is one way to do it, but that will stop execution of any subsequent code until the call completes.

Another (and the more correct, I think) way is to make the call asynchronously, and put the code you want to execute after in the onComplete handler.

RedFilter
Is the onComplete handler part of a framework, or can I just use it from "plain" javascript?
Carvell Fenton
The handlers are implemented differently in different libraries, the spec is here though: http://www.w3.org/TR/XMLHttpRequest2/#event-handler-attributes
RedFilter
I use Prototype, they call their handler onSuccess: http://www.prototypejs.org/learn/introduction-to-ajax
RedFilter
A: 

http://docs.jquery.com/Ajax/jQuery.getJSON#urldatacallback <-- Take a look at the callbacks with jQuery

Also look at the Start \ Stop http://docs.jquery.com/Ajax/ajaxStart#callback and http://docs.jquery.com/Ajax/ajaxStop#callback - you can execute javascript to do anything you require....

Ben Hall
+1  A: 

How many levels deep are the chained selects? I'm doing this with only 2 levels (1 ajax driven select) and for the second page load I would add some code to my jquery document ready function that would initiate the ajax call to load the select list but also send it the id of the field I wanted selected so when the ajax call completes it will have loaded my select and also set it's necessary value.

CRasco
Sounds like you are using jQuery, and I am not. However, based on the responses I am getting pointing to using framework functions, perhaps I should be using one!
Carvell Fenton
There is really no reason not to be using a JS Framework. It really simplifies your "ajax" calls.
CRasco
I was concerned that I would fall into the trap of not really understanding AJAX if I defaulted to using a framework, but perhaps I should move to one now. Would you recommend jQuery as the "best" one for me to use?
Carvell Fenton
I'm not sure if it's "best", but I'm almost certain it's "good enough"
Don
I prefer jQuery to prototype and mootools and the reason you want to use a framework for AJAX is that it handles most of the browser quirks behind the scenes and shrinks your code from 50-100 lines including callbacks to maybe 10-20 lines. Well worth it.
CRasco
A: 

Use the onreadystatechange event listener.

Jeremy Stein
I am using onreadystatechange and setting it to a function that handles the responseText. That is the function where I populate the select elements with the option values returned from php. I could pass the selected option back from php in addition to the list of available options and set the matching option to be selected. There is always something selected by the time I call the php. I think this is along the same lines as what CRasco is suggesting, but without the framework. Just not sure if adding the framework would be beneficial to me in the long run...?
Carvell Fenton
I'm confused. I think you need to post some code.
Jeremy Stein
A: 

Thanks for all the answers! Once again the Stackoverflow folks have enlightened me. Based on the comments, I think I have several options:

(1) I can avoid a framework and just pass more information to be processed in the onreadystatechange event listener (I think this is what Jeremy Stein was suggesting, even though my comment confused the issue). In this case, when I am dynamically adding the option elements to the select, I would just set one of them to selected. In that case I have to pass the selected option (via URL I guess) to the php file, then have the php file include that in the built responseText so I know which option to set as selected in the onreadystatechange listener.

(2) I can use an AJAX call to wrap the form post and never leave the form page (as Mikeb suggested).

(3) I can add in a framework that does the work for me (CRasco, Ben Hall, OrbMan).

I have spent the last few hours trying to understand the pros and cons of jQuery, Prototype, and MooTools to see which is easier for me to implement and best suits my needs. I have been successful in getting a very basic form page behaving close to what I want with jQuery. It carries out the process and never leaves the form page, so it is options 2 and 3 together I think. Now I just have to figure out a bit more about how the $.ajax call actually works and where I can get the responseText from if I need it. I can look that up though :)

So thanks again all.

What are you supposed to do on here when more than one answer pointed you in the right direction?

Carvell Fenton
The important thing is for the answer to the question to be the accepted answer. If none of the answers actually solved your problem, post your own answer and accept it. If you want to acknowledge those who helped you, give them an upvote.
Jeremy Stein