943,712 Members | Top Members by Rank

Ad:
Aug 6th, 2008
0

Help with Drop down list

Expand Post »
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>
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
BillyMako is offline Offline
16 posts
since Jan 2008
Aug 6th, 2008
0

Re: Help with Drop down list

its so easy
Reputation Points: 15
Solved Threads: 21
Posting Whiz in Training
nikesh.yadav is offline Offline
219 posts
since Feb 2008
Aug 6th, 2008
0

Re: Help with Drop down list

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!!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
BillyMako is offline Offline
16 posts
since Jan 2008
Aug 6th, 2008
0

Re: Help with Drop down list

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
Reputation Points: 30
Solved Threads: 36
Posting Whiz
langsor is offline Offline
389 posts
since Aug 2008

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: suggest a tool to make javascript unreadable
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: problem with date validation





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


Follow us on Twitter


© 2011 DaniWeb® LLC