JayDude Posted March 27, 2012 Share Posted March 27, 2012 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"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/259837-help-needed-with-drop-down-list/ Share on other sites More sharing options...
mofm Posted March 27, 2012 Share Posted March 27, 2012 ok u need to use ajax to do what u want. have a look at http://www.w3schools.com/PHP/php_ajax_database.asp to get you started with ajax Quote Link to comment https://forums.phpfreaks.com/topic/259837-help-needed-with-drop-down-list/#findComment-1331727 Share on other sites More sharing options...
Psycho Posted March 27, 2012 Share Posted March 27, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/259837-help-needed-with-drop-down-list/#findComment-1331730 Share on other sites More sharing options...
mofm Posted March 27, 2012 Share Posted March 27, 2012 Although you can just use JS, I think the OP will benefit more from learning AJAX. Quote Link to comment https://forums.phpfreaks.com/topic/259837-help-needed-with-drop-down-list/#findComment-1331733 Share on other sites More sharing options...
Psycho Posted March 27, 2012 Share Posted March 27, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/259837-help-needed-with-drop-down-list/#findComment-1331741 Share on other sites More sharing options...
JayDude Posted March 27, 2012 Author Share Posted March 27, 2012 Since I am a newby to php, I am certain that many pitfalls will be in my path - but a good heads-up is always welcome. I will use the info to concider speed over quality Thank you For now my problem is sorted, thanx Quote Link to comment https://forums.phpfreaks.com/topic/259837-help-needed-with-drop-down-list/#findComment-1331750 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.