943,192 Members | Top Members by Rank

Ad:
You are currently viewing page 1 of this multi-page discussion thread
Nov 18th, 2009
0

Select List/Menu Help

Expand Post »
I am New in web designing
What i want is i have one List/Menu its called Make it's containing some vehicles Brands when i selected a brand name i want another list/menu display the models that relevant to Brand

this is a screen shot that what i need


some of vehicle websites are using this thing

Here is my codes

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>Untitled Document</title>
  6. </head>
  7.  
  8. <body><form action="" method="post" name="F2" id="F2">
  9. <p>Make
  10. <select name="list" id="list">
  11. <option value="DESOTO ">DESOTO </option>
  12. <option value="DODGE ">DODGE </option>
  13. <option value="DUTTON ">DUTTON </option>
  14. <option value="EDSEL ">EDSEL </option>
  15. <option value="ELFIN ">ELFIN </option>
  16. <option value="EUNOS ">EUNOS </option>
  17. <option value="FERRARI ">FERRARI </option>
  18. <option value="FIAT ">FIAT </option>
  19. <option value="FORD ">FORD </option>
  20. </select>
  21. </p>
  22. <p>Model
  23. <select name="select" id="select">
  24. <option>SELECT</option>
  25. </select>
  26. </p>
  27. </form>
  28. </body>
  29. </html>
Similar Threads
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
ruwanaru is offline Offline
67 posts
since Mar 2009
Nov 18th, 2009
0
Re: Select List/Menu Help
Do you want to after- or pre-load your models? This can be done in two ways you see, the first one is to wait for a user to select a brand, then request from the server what models exist for that brand, the other one is that you load all at the beginning, then you filter through that list using javascript. Both are pretty easy to use, but the first one requires you to know some server-scripting (like php or asp or such).
Reputation Points: 15
Solved Threads: 10
Junior Poster in Training
Alxandr is offline Offline
73 posts
since May 2009
Nov 18th, 2009
0
Re: Select List/Menu Help
Click to Expand / Collapse  Quote originally posted by Alxandr ...
Do you want to after- or pre-load your models? This can be done in two ways you see, the first one is to wait for a user to select a brand, then request from the server what models exist for that brand, the other one is that you load all at the beginning, then you filter through that list using javascript. Both are pretty easy to use, but the first one requires you to know some server-scripting (like php or asp or such).
i like to know borth of them ithink using the javascript is fine
Last edited by ruwanaru; Nov 18th, 2009 at 1:44 am.
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
ruwanaru is offline Offline
67 posts
since Mar 2009
Nov 18th, 2009
1
Re: Select List/Menu Help
All of them would require some use of JavaScript. But I can at least get you started. Since you haven't stated anything about any other use of Javascript I'll suspect that you're not to familiar with the subject, and I'll also use mootools for the code-snippets I'll post here.

JavaScript Syntax (Toggle Plain Text)
  1. function buildSelect(select, options)
  2. {
  3. var select = $(select);
  4. select.empty();
  5. options.each(function(item) {
  6. if($type(item) != "array")
  7. {
  8. item = [item, item];
  9. var option = new Element("option", {
  10. text: item[0].toString(),
  11. value: item[1].toString()
  12. });
  13. option.inject(select);
  14. }
  15. });
  16. }

The following code will take a javascript-array (eather a simple array or an array of array consisting of [text, value] pairs) and populate an select with them. So that's the basic of your code.

Now some html, you wanted 2 selects, one master and one slave:

html Syntax (Toggle Plain Text)
  1. <select id="master">
  2. <option value="Audi">Audi</option>
  3. <option value="BMW">BMW</option>
  4. </select>
  5. <select id="slave">
  6. </select>

Pretty straight forward so far, now let's make a domready function. If you're not familiar with domready it's a bit like onload, but it's fired before the onload event. domready is fired when the DOM-model is ready-built and is a mootools-spesific thing.

JavaScript Syntax (Toggle Plain Text)
  1. function domready()
  2. {
  3. $('master').addEvent('change', master_changed);
  4. }

That is all we need for our domready function. Now we need to build the master_changed function which will be fired every time the master-select is changed. But here is where we need to determine what method to use, the preloaded or the lateloaded method. This one will be using the lateloaded.

JavaScript Syntax (Toggle Plain Text)
  1. function master_changed()
  2. {
  3. var req = new Request.JSON({
  4. url: 'getmodels.php',
  5. method: 'post',
  6. data: 'brand=' + encodeURIComponent($('master').get('value'))
  7. });
  8. req.addEvent('success', function(response) {
  9. buildSelect('slave', response);
  10. });
  11. req.send();
  12. }

The other way is to preload, to do that you initially have to build up an array of what brands have what models. This can be done as following:

JavaScript Syntax (Toggle Plain Text)
  1. var cars = {
  2. "BMW": ["Z3", "Z4"],
  3. "Audi": ["A4", "A3"]
  4. }

Then the master_changed-function needs to be updated to this:
JavaScript Syntax (Toggle Plain Text)
  1. function master_changed()
  2. {
  3. var s = $('master').get('value').trim();
  4. buildSelect('slave', cars[s]);
  5. }

The last thing you need to do is to make shure that the domready-function is ever called. You do this simply by adding the line window.addEvent('domready', domready); outside of all the functions.

HTH
Reputation Points: 15
Solved Threads: 10
Junior Poster in Training
Alxandr is offline Offline
73 posts
since May 2009
Nov 18th, 2009
0
Re: Select List/Menu Help
thank you for the help but im confused with all of this things can you give little bit of help by giving me full working code i did what did you say something wrong with me sry abut the disturb
thank you
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
ruwanaru is offline Offline
67 posts
since Mar 2009
Nov 18th, 2009
0
Re: Select List/Menu Help
Well, all the code I've given you is working, it's just about puting it together.

Opt 1, only javascript:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. function buildSelect(select, options)
  2. {
  3. var select = $(select);
  4. select.empty();
  5. options.each(function(item) {
  6. if($type(item) != "array")
  7. {
  8. item = [item, item];
  9. var option = new Element("option", {
  10. text: item[0].toString(),
  11. value: item[1].toString()
  12. });
  13. option.inject(select);
  14. }
  15. });
  16. }
  17.  
  18. function domready()
  19. {
  20. $('master').addEvent('change', master_changed);
  21. }
  22.  
  23. var cars = {
  24. "BMW": ["Z3", "Z4"],
  25. "Audi": ["A4", "A3"]
  26. }
  27.  
  28. function master_changed()
  29. {
  30. var s = $('master').get('value').trim();
  31. buildSelect('slave', cars[s]);
  32. }

Opt 2, using server-callback:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. function buildSelect(select, options)
  2. {
  3. var select = $(select);
  4. select.empty();
  5. options.each(function(item) {
  6. if($type(item) != "array")
  7. {
  8. item = [item, item];
  9. var option = new Element("option", {
  10. text: item[0].toString(),
  11. value: item[1].toString()
  12. });
  13. option.inject(select);
  14. }
  15. });
  16. }
  17.  
  18. function domready()
  19. {
  20. $('master').addEvent('change', master_changed);
  21. }
  22.  
  23. function master_changed()
  24. {
  25. var req = new Request.JSON({
  26. url: 'getmodels.php',
  27. method: 'post',
  28. data: 'brand=' + encodeURIComponent($('master').get('value'))
  29. });
  30. req.addEvent('success', function(response) {
  31. buildSelect('slave', response);
  32. });
  33. req.send();
  34. }

Html:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>Untitled Document</title>
  6. </head>
  7.  
  8. <body><form action="" method="post" name="F2" id="F2">
  9. <p>Make
  10. <select name="list" id="master">
  11. <option value="Audi">Audi</option>
  12. <option value="BMW">BMW</option>
  13. </select>
  14. </p>
  15. <p>Model
  16. <select name="select" id="slave">
  17. <option>SELECT</option>
  18. </select>
  19. </p>
  20. </form>
  21. </body>
  22. </html>

But as said you're required to use the mootools-framework which can be found at mootools.net.
Last edited by Alxandr; Nov 18th, 2009 at 4:02 am.
Reputation Points: 15
Solved Threads: 10
Junior Poster in Training
Alxandr is offline Offline
73 posts
since May 2009
Nov 18th, 2009
0
Re: Select List/Menu Help
how to use this mootool
i don't know any thing about that
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
ruwanaru is offline Offline
67 posts
since Mar 2009
Nov 18th, 2009
0
Re: Select List/Menu Help
Click to Expand / Collapse  Quote originally posted by ruwanaru ...
how to use this mootool
i don't know any thing about that
You download this file (which is mootools), add it to your webpages and add a reference to it in the head-section of the html-document using the code:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <script language="javascript" type="text/javascript" src="pathtomootoolsfile.js"></script>
Reputation Points: 15
Solved Threads: 10
Junior Poster in Training
Alxandr is offline Offline
73 posts
since May 2009
Nov 18th, 2009
0
Re: Select List/Menu Help
Click to Expand / Collapse  Quote originally posted by Alxandr ...
You download this file (which is mootools), add it to your webpages and add a reference to it in the head-section of the html-document using the code:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <script language="javascript" type="text/javascript" src="pathtomootoolsfile.js"></script>
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>Untitled Document</title>
  6. <script language="javascript" type="text/javascript" src="pathtomootoolsfile.js"></script>
  7. <script type="text/javascript">
  8.  
  9. function buildSelect(select, options)
  10. {
  11. var select = $(select);
  12. select.empty();
  13. options.each(function(item) {
  14. if($type(item) != "array")
  15. {
  16. item = [item, item];
  17. var option = new Element("option", {
  18. text: item[0].toString(),
  19. value: item[1].toString()
  20. });
  21. option.inject(select);
  22. }
  23. });
  24. }
  25.  
  26. function domready()
  27. {
  28. $('master').addEvent('change', master_changed);
  29. }
  30.  
  31. var cars = {
  32. "BMW": ["Z3", "Z4"],
  33. "Audi": ["A4", "A3"]
  34. }
  35.  
  36. function master_changed()
  37. {
  38. var s = $('master').get('value').trim();
  39. buildSelect('slave', cars[s]);
  40. }
  41.  
  42.  
  43. function buildSelect(select, options)
  44. {
  45. var select = $(select);
  46. select.empty();
  47. options.each(function(item) {
  48. if($type(item) != "array")
  49. {
  50. item = [item, item];
  51. var option = new Element("option", {
  52. text: item[0].toString(),
  53. value: item[1].toString()
  54. });
  55. option.inject(select);
  56. }
  57. });
  58. }
  59.  
  60. function domready()
  61. {
  62. $('master').addEvent('change', master_changed);
  63. }
  64.  
  65. function master_changed()
  66. {
  67. var req = new Request.JSON({
  68. url: 'getmodels.php',
  69. method: 'post',
  70. data: 'brand=' + encodeURIComponent($('master').get('value'))
  71. });
  72. req.addEvent('success', function(response) {
  73. buildSelect('slave', response);
  74. });
  75. req.send();
  76. }
  77. </script>
  78. </head>
  79.  
  80. <body><form action="" method="post" name="F2" id="F2">
  81. <p>Make
  82. <select name="list" id="master">
  83. <option value="Audi">Audi</option>
  84. <option value="BMW">BMW</option>
  85. </select>
  86. </p>
  87. <p>Model
  88. <select name="select" id="slave">
  89. <option>SELECT</option>
  90. </select>
  91. </p>
  92. </form>
  93. </body>
  94. </html>


is this correct this is not working
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
ruwanaru is offline Offline
67 posts
since Mar 2009
Nov 18th, 2009
0
Re: Select List/Menu Help
Yes it's correct except you have to replace the pathtomootoolsfile.js with the actual path to the mootools file.

EDIT:
No, wait a second, you can't use both methods. I sad eater Opt1 OR Opt2. You can't use both at the same time.
Last edited by Alxandr; Nov 18th, 2009 at 4:47 am.
Reputation Points: 15
Solved Threads: 10
Junior Poster in Training
Alxandr is offline Offline
73 posts
since May 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: onclick function is not working (plz help)
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Script Not Working in Other Browser except IE





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC