Question with AJAX

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I am still improving my AJAX and I ran into something that I'm not sure
how to do.

I will have a page called where I will have a dropdown list at the top
and a large display at the bottom.  The display depends upon what is
currently selected in the dropdown.  This is readily done via AJAX which
is activated with an onchange in the dropdown list.  The contents of the
bottom half of the page are created in the php script which is sent back
to the response handler which changed the bottom part via an innerHTML.

So far OK.  What I would also like to happen is that when the page is
called initially, that the bottom half shows for the first entry of the
dropdown list.  I don't want to repeat code in two places.

One solution I have is to have the entire selection and bottom building
part be an include file that I put into both my AJAX server php script
and into this display page.  I would just use different values for the
selected item.  It would be on the display page as a require_once before
I do any html display.

My question is whether there is some better way.

Re: Question with AJAX

On Jun 3, 11:45=A0am, sheldonlg <sheldonlg> wrote:
Quoted text here. Click to load it

Is the dropdown list dynamic or static?

If it is static and you are using a GET method (which seems logical, I
don't  believe you are manipulating any data, merely retrieving it)
then why not just initiate the page using your script passing it the
desired value?

Re: Question with AJAX

after9 wrote:
Quoted text here. Click to load it

It is dynamic.  Once in the page, a list is build from the category
passed in.  It is that list that is then used to display what is in the
bottom of the page.  The first llist is a db call.  The second list is
also a db call using the value from the first list.  Coming into the
page, we have no idea as to what the id will be for the first member on
the first list.

The more I think of it, the more the require_once of the building code
looks like the way to go.  There is no duplication of code (one include
file) and it is just exercised in two different places.

Re: Question with AJAX

On Jun 3, 12:31=A0pm, sheldonlg <sheldonlg> wrote:
Quoted text here. Click to load it

So let me get the workflow straight.

1. A user selects a category.
2. This category is passed to the display page.
3. On display page, a dropdown list is populated via a database query
of category that was passed.
4. On a dropdown change event a request is passed to a php script.
5. The php script returns database information to display page.

When #3 happens you want the display section to automatically load the
information of the first dropdown option of the category.

One solution involves the steps prior to #3.  Along with passing the
category, why not pass along the first item in category's database?
Then you could have a GET request of something like display.php?
category=3Dfoo&first=3Dbar.  In the display page you could check if
$_GET["first"] is set and then handle it appropriately.

Re: Question with AJAX

after9 wrote:
Quoted text here. Click to load it

...because I don't KNOW the first item before getting to the page and
running the first db query to populate the list of items.  The items are
identified by an ID which is obtained from the db based up the category ID.

Re: Question with AJAX

On Jun 3, 12:31=A0pm, sheldonlg <sheldonlg> wrote:
Quoted text here. Click to load it

In the code that populates your dropdown, after populating the
dropdown simply set the selectedIndex of it to 0 and then call the
function that is called by the onchange handler, or you can even call
onchange() of the element.

function loadMyDropDown() {
    var select = document.getElementById("categories");
    //...populate the dropdown...
    if (select.options.length > 0) {
        select.selectedIndex = 0;


Re: Question with AJAX

the bad thing about toms method is, if javascript is deactivated the
user will see nothing. I favoured your solution when I had the problem
first, for me it worked well. One tip about it: i would ad an ?ajax to
all ajax calls, e.g. includes/ajax3.php?ajax
by using if(isset($_GET['ajax'])) you can check if your file is called
by ajax or if it is included. So if you have differences in some
pathes or anything else you can avoid problems with this method

Re: Question with AJAX wrote:
Quoted text here. Click to load it

Great suggestion.

Re: Question with AJAX

Tom Cole wrote:
Quoted text here. Click to load it

That is essentially to the way I now handle it.  I took the bulk of the
called function and put it in a separate php file that is included in
the called function.  What is not in that include file is the grabbing
of the category ID.  The category ID is obtained from a $_GET from the
AJAX call.  In the display page I also include that included file, but
use as the category the first entry from the dropdown list.  That way I
have no duplication of code.

Quoted text here. Click to load it

Site Timeline