473,385 Members | 1,944 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

how to change 2nd drop down contens when a item selected from first dropdown list ?

4
hi every one im using php for developing a project.
now im having big problem,
i have three drop down lists(lets say DD1,DD2,DD3) they will show item from database.
for example DD1 have(it will load from database AND DD2,DD3 remain empty @ beginning) Books,Cars,Films.
assume that i have selected Cars then DD2 have to show Sports car, Regular cars,..ect.
Like that if i select an item from DD2 the DD3 should change according to that.


can anyone explain how can i complete this task???

thanks in advance.
Apr 3 '08 #1
9 6940
Markus
6,050 Expert 4TB
hi every one im using php for developing a project.
now im having big problem,
i have three drop down lists(lets say DD1,DD2,DD3) they will show item from database.
for example DD1 have(it will load from database AND DD2,DD3 remain empty @ beginning) Books,Cars,Films.
assume that i have selected Cars then DD2 have to show Sports car, Regular cars,..ect.
Like that if i select an item from DD2 the DD3 should change according to that.


can anyone explain how can i complete this task???

thanks in advance.
You'd use an ajax call to query a seperate php page and then have the results of the php page populate the drop down.
You'll have to read a tutorial on ajax - w3schools has a good one
Apr 3 '08 #2
nixan
4
thanks markusn00b,
but i dont have any idea about AJAX & i didnt use any coding in AJAX.
cant it done by php itself?
Apr 3 '08 #3
Atli
5,058 Expert 4TB
Hi.

PHP can not edit anything client-side once the page has been loaded. So, it can't be used to respond to any client-side events, such as drop-down changes.

Markus' suggestion would be my nr. 1 solution as well. It simply makes a HTTP request, requesting additional data from a PHP document. (See w3schools.com for a simple AJAX tutorial)

You could of course have PHP create a Javascript array, containing the data, and have Javascript switch out the data when the drop-down boxes change. I wouldn't advice this tho if you have loads of data to handle, the AJAX solution would be better.
Apr 3 '08 #4
Markus
6,050 Expert 4TB
thanks markusn00b,
but i dont have any idea about AJAX & i didnt use any coding in AJAX.
That is why you read tutorials.

Regards.
Apr 3 '08 #5
ronverdonk
4,258 Expert 4TB
That is why you read tutorials.

Regards.
Like the following one on Ajax chained select

Ronald
Apr 3 '08 #6
coolsti
310 100+
Doh to me. All the time I have been reading everyone's jibe about Ajax and not knowing what in the world this is. So I took a look to see just now.

You can do the very same thing (maybe with a bit more coding effort on your part) by just including a hidden iframe on your main page calling its own separate php scripts that do all your behind the scenes database queries and other tasks. So if you really do not wish to get into Ajax, just use a combo of a hidden iframe and some javascript to do needed behind the scenes queries and repopulations of main page drop down lists.

There is also another way to handle the parent -> child drop down list thing, which does not require background queries and which I use a lot in my application. You basically get all the possible information for the drop down lists from the database when you present the main page, and store them in javascript arrays, and then let javascript repopulate the drop down lists using the "onchange" event. This works well when the number of choices and number of lists in the chain are not too large. It then does not require any client server interaction (and avoids that latency) when the user is clicking around in the lists. To set this up does require a bit of javascript but it works nicely. If you would use this often, you can create some PHP class that creates and manages the creation of HTML and Javascript code for parent->child or parent->child->child lists. I did this some time back and now it is a breeze for me to include a new one on a new page.
Apr 4 '08 #7
Expand|Select|Wrap|Line Numbers
  1. <!--
  2. ----------------------------
  3. database: demo1;
  4. table: demo_table;
  5. fields(demo_table)
  6.     item varchar(20);
  7.     list varchar(20);
  8. ----------------------------
  9. -->
  10. <html>
  11. <head>
  12. <title>Untitled Document</title>
  13. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  14. <script type="text/javascript">
  15. function getItem(sel)
  16. {
  17.     var itemCode = sel.options[sel.selectedIndex].value;
  18.     //alert(itemCode);
  19.      var url= "http://localhost/onchange1.php?item="+itemCode;
  20. window.location=url;
  21.  
  22. }
  23.  
  24. </script>
  25. </head>
  26.  
  27. <body>
  28.  
  29. <form action="" method="post">
  30. <table>
  31.     <tr>
  32.         <td>Item </td>
  33.         <td><select id="item" name="item" onchange="getItem(this)">
  34.             <option value="">Select </option>
  35.             <option value="pr">Printer</option>
  36.             <option value="ri" >Ribbon</option>
  37.                 </select>
  38.         </td>
  39.     </tr>
  40.     <tr>
  41.         <td>List </td>
  42.         <td><select id="item_list" name="item_list"  >
  43.         <option value="">Select </option>
  44.         <?php
  45. $i_code=$_REQUEST['item'];
  46.  
  47. $con = mysql_connect("localhost","root","");
  48. mysql_select_db("demo1",$con);
  49.  
  50. $query="select list from demo_table where item='$i_code'";
  51. $res=mysql_query($query);
  52. $num=mysql_num_rows($res);
  53. for($i=0;$i<$num;$i++)
  54. {
  55. $data=mysql_fetch_array($res);
  56. $v=$data[0].$i;
  57.  echo "<option value='$data[0]'>".$data[0]."</option>";
  58. }
  59. ?>
  60. </select>
  61. </td>
  62. </tr>
  63. </table>
  64. </form>
  65. </body>
  66. </html>
  67.  
Apr 5 '08 #8
Atli
5,058 Expert 4TB
Hi,sethupnr.

Please use [code] tags when posting code examples. Failing to do so will make your code next to unreadable and adds extra work for us moderators who have to clean up after you.

[code] ...Code goes here... [/code]
[code=php] ...PHP code goes here... [/code]

Thank you.
Apr 5 '08 #9
nixan
4
hi thanks for reply to my problem.
special thanks to sethupnr its very useful to me as a begginner. and also thanks to markusn00b, for gude me to study about AJAX.

thanks.
Apr 7 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Paul M | last post by:
Hi folks, When I set the datasource of a dropdown list and bind it, the drop down list always defaults to the first item in the list. I need it to default to Nothing or NULL or whatever it is...
4
by: Merdaad | last post by:
My drop down list is populated from a static array in my codebehind (c#) code. I set the selected index based on some known values(from DB), when the screen shows up, dropdown list shows that the...
2
by: Yoshitha | last post by:
hi I have 2 drop down lists in my application.1st list ontains itmes like java,jsp,swings,vb.net etc.2nd list contains percentage i.e it conatains the items like 50,60,70,80,90,100. i will...
0
by: BryanS | last post by:
I am having trouble trying to link 2 drop down lists and a repeater control. What i want is 2 drop down lists, the first being a list of food categories. When a category is selected the second...
6
by: Rob Meade | last post by:
Hi all, Looking for a bit of help if possible. For about 2 weeks now I've been investigating the best way to populate related drop down menus, and have their values pre-populated again if the...
6
by: zacks | last post by:
I have an application I am developing that has a Combo Box that is intended to show a list of available tables in the selected DSN. I have put code in the control's DropDown event handler to clear...
1
by: Valli | last post by:
Hi, I need to populate a dropdownlist control with values. Number of items for that list exceeds 100. I am using Html select list option. When I drop down the list control, it shows the item...
11
by: tokcy | last post by:
Hi everyone, I am new in php and ajax, i am facing the prob while i click on element of first drop down then in second dropdown all element showl come from database. I mean i have three dropdown 1....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.