Jump to content

Help needed with Drop down list


JayDude

Recommended Posts

I have two basic dropdown lists in a form, my question is this:

 

After selecting an item from the 1st dropdown list, the second dropdown list should automatically load only fields that are connected with the first selected item. (ex Site1 has Tank01 and Tank06, but Site2 has Tank03, Tank04 and also a Tank06).

 

What is the script needed / or how do I change my script to accommodate this?

 

Here is the basic script I use for the second dropdown list:

 

<?php

include("../xxx.php");

$cxn = mysqli_connect($host,$user,$password,$dbname);

$query = "SELECT DISTINCT `diesel_tank_id` FROM `diesel_tank`

ORDER BY `diesel_tank_id`";

$result = mysqli_query($cxn,$query);

while($row = mysqli_fetch_assoc($result))

{

extract($row);

echo "<option value='$diesel_tank_id'>$diesel_tank_id</option>\n";

}

?>

Link to comment
Share on other sites

Well, you don't necessarily need to use AJAX, but you definitely want to use JavaScript. If the lists of all possible options is not excessive, then you can simply load all the data into the page as JavaScript data and have all the functionality happen client-side. However, if the list of all options is very large or if the available options can change based upon other activities then you would want to use AJAX.

 

I know I have several posts in these forums providing a JavaScript only solution. Here is one: http://www.phpfreaks.com/forums/index.php?topic=148720.msg640073#msg640073

Link to comment
Share on other sites

Although you can just use JS, I think the OP will benefit more from learning AJAX.

 

Perhaps, but the question wasn't about what new technology can I learn to solve this problem, it was about "how do I solve this problem". AJAX has many benefits, but it also has it's drawbacks. If you use it to populate "linked" select lists a request must be sent back to the server each time a primary select list is changed and then the results are sent back to the client to be used to update the child select list(s). That process will take some time. Depending on many factors that process can be very quick - so much so it is imperceptible to the user - or the process can take a second or more. A second or two may not seem like much, but from a user's perspective it makes the site seem slow or buggy. In fact, if there are times when the whole process may hang due to a blip in transmission.

 

In addition, since there will be some delay that you cannot know ahead of time you would have to implement some checks and balances into the page. For example, what if a user changes the first option and then, before the 2nd list has been updated by the AJAX call, the user changes the value in the 2nd list? You would have to disable the 2nd list before the AJAX call is made and then re-enable after the list is updated. I have personally seen where this had to be done because the AJAX call could take a couple seconds to complete.

 

If you have a somewhat finite list of options the best option, in my opinion, is to simply implement it completely client-side. The performance will be instantaneous and you don't have to worry about all the other problems.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.