Help with Drop down list

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Reply

Join Date: Jan 2008
Posts: 16
Reputation: BillyMako is an unknown quantity at this point 
Solved Threads: 0
BillyMako BillyMako is offline Offline
Newbie Poster

Help with Drop down list

 
0
  #1
Aug 6th, 2008
I want to change this code so that you can enter "Fruits" in a textr field and the drop down is populated instead of selecting "Fruits" from a drop down.

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <!doctype html public "-//w3c//dtd html 3.2//en">
  2.  
  3. <html>
  4.  
  5. <head>
  6. <title>Multiple drop down list box from plus2net</title>
  7. <SCRIPT language=JavaScript>
  8. function reload(form)
  9. {
  10. var val=form.cat.options[form.cat.options.selectedIndex].value;
  11. self.location='dd.php?cat=' + val ;
  12. }
  13.  
  14. </script>
  15. </head>
  16.  
  17. <body>
  18. <?
  19.  
  20. /*
  21. If register_global is off in your server then after reloading of the page to get the value of cat from query string we have to take special care.
  22. To read more on register_global visit.
  23.   http://www.plus2net.com/php_tutorial/register-globals.php
  24. */
  25. @$cat=$_GET['cat']; // Use this line or below line if register_global is off
  26. //@$cat=$HTTP_GET_VARS['cat']; // Use this line or above line if register_global is off
  27.  
  28. ///////// Getting the data from Mysql table for first list box//////////
  29. $quer2=mysql_query("SELECT DISTINCT category,cat_id FROM category order by category");
  30. ///////////// End of query for first list box////////////
  31.  
  32. /////// for second drop down list we will check if category is selected else we will display all the subcategory/////
  33. if(isset($cat) and strlen($cat) > 0){
  34. $quer=mysql_query("SELECT DISTINCT subcategory FROM subcategory where cat_id=$cat order by subcategory");
  35. }else{$quer=mysql_query("SELECT DISTINCT subcategory FROM subcategory order by subcategory"); }
  36. ////////// end of query for second subcategory drop down list box ///////////////////////////
  37.  
  38. echo "<form method=post name=f1 action='dd-check.php'>";
  39. /// Add your form processing page address to action in above line. Example action=dd-check.php////
  40. ////////// Starting of first drop downlist /////////
  41. echo "<select name='cat' onchange=\"reload(this.form)\"><option value=''>Select one</option>";
  42. while($noticia2 = mysql_fetch_array($quer2)) {
  43. if($noticia2['cat_id']==@$cat){echo "<option selected value='$noticia2[cat_id]'>$noticia2[category]</option>"."<BR>";}
  44. else{echo "<option value='$noticia2[cat_id]'>$noticia2[category]</option>";}
  45. }
  46. echo "</select>";
  47. ////////////////// This will end the first drop down list ///////////
  48.  
  49. ////////// Starting of second drop downlist /////////
  50. echo "<select name='subcat'><option value=''>Select one</option>";
  51. while($noticia = mysql_fetch_array($quer)) {
  52. echo "<option value='$noticia[subcategory]'>$noticia[subcategory]</option>";
  53. }
  54. echo "</select>";
  55. ////////////////// This will end the second drop down list ///////////
  56. //// Add your other form fields as needed here/////
  57. echo "<input type=submit value=Submit>";
  58. echo "</form>";
  59. ?>
  60. <center><a href='http://www.plus2net.com'>PHP SQL HTML free tutorials and scripts</a></center>
  61. </body>
  62.  
  63. </html>
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 213
Reputation: nikesh.yadav is an unknown quantity at this point 
Solved Threads: 17
nikesh.yadav's Avatar
nikesh.yadav nikesh.yadav is offline Offline
Posting Whiz in Training

Re: Help with Drop down list

 
0
  #2
Aug 6th, 2008
its so easy
Help as an alias

I think programming is great................
Tour Travel weblink by me and about Tour ,
Go To My Home Page and I m in Webdevelopment.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 16
Reputation: BillyMako is an unknown quantity at this point 
Solved Threads: 0
BillyMako BillyMako is offline Offline
Newbie Poster

Re: Help with Drop down list

 
0
  #3
Aug 6th, 2008
Originally Posted by nikesh.yadav View Post
its so easy
Please help me then!!!

I want to eneter "Fruits" into a text field and then for the Fruit options to populate the 2nd drop down menu.

So all i want to do is replace the 1st drop down menu with a text field where i can manually enter a category.

Thankyou!!
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 381
Reputation: langsor is an unknown quantity at this point 
Solved Threads: 33
langsor langsor is offline Offline
Posting Whiz

Re: Help with Drop down list

 
0
  #4
Aug 6th, 2008
Hi,

The challenge with what you are asking is that you must rely on what the user enters into the text field and that data must match exactly what the search term is from the database record ... does that make sense?

So if someone enters Fruit, friut, fruits in the text field, none of them will match up correctly with fruit which is the matching field to populate your subcategory list.

You can try to manage some of this concern using javascript validation and post-processing ...
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. var cat = document.getElementById('cat').value;
  2. if ( /fruit/i.test( cat ) ) {
  3. cat = 'fruit';
  4. }
This will remedy cases like Fruit and fruits but not transposed letters or miss spellings like friut or frute ... which are both actually quite common problems.

From here you will need to populate the form variable to be submitted with the identified 'cat' value and then activate or submit the form with javascript -- which means all of this must be processed before the form is submitted with the existing script on your page.

I'm not really willing to sit here and rewrite this entire page to allow text field entry of options, but if you decide to take this on, I will be glad to give you pointers along the way.

Good luck
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC