By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,590 Members | 663 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,590 IT Pros & Developers. It's quick & easy.

need help with auto load of listbox contents without page refresh or button

100+
P: 116
i have 2 tables 1 is status_type with field name status and other is users with field username and status

now i want that the first listbox lists all status from status type ( this i have achieved with php )

and based on selection of the value in listbox 1 the corresponding data in table users be loaded in listbox 2 .

i have achieved this by using a button in php but i want tht it operate without submit button and no page refreshing

something like whats shown in http://www.plus2net.com/php_tutorial/dd.php i donno javascript so cant figure out wat and where to make the changes

here is my code which works with a submit button in php...plz help me out

Expand|Select|Wrap|Line Numbers
  1.  
  2. <?php
  3. //------------------------------------------------------------
  4. // Part 1: Choosing a Status
  5. //============================================================
  6.  
  7. // Initializes a list of acceptable statuses
  8. $status_list = array();
  9.  
  10. $query_status = " SELECT status_type.status from status_type ";
  11.  
  12.        $result_status = mysql_query($query_status);
  13.         confirm_query($result_status);
  14.  
  15.  
  16.  
  17.         $status_list[]=" ";
  18.   while ($record = mysql_fetch_assoc($result_status)) {
  19.           $status_list[] = $record['status'] ;
  20.  
  21.         }
  22. $status = $_GET['status'];
  23.  
  24.  
  25.  
  26. // A form for choosing the status
  27. ?>
  28. <form method="get" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>">
  29.     <input type="submit" value="Search for status:" />
  30.     <select name="status" >
  31.         <?php
  32.  
  33.         // Loops through the $status_list array
  34.          foreach ($status_list as $value => $option)
  35.         {
  36.             // Sets an attribute to show the chosen status as selected
  37.             $selected = ($status == $value) ? ' selected="selected"' : '';
  38.            //$status = (isset($_get['status'])) ? $_get['status'] : "";
  39.  
  40.             // Builds an option for each acceptable status
  41.            echo '<option value="'.$value.'"'.$selected.'>'.$option.'</option>';
  42.            // echo '<option value="'.$value.'"'.$status.'>'.$option.'</option>';
  43.         }
  44.         ?>
  45.     </select>
  46. </form>
  47.  
  48. <?php
  49. //------------------------------------------------------------
  50. // Part 2: Using the Chosen Status to Search the Database
  51. //============================================================
  52.  
  53. // Initializes an empty array of usernames
  54. $usernames = array();
  55.  
  56. // Builds a query to get the names of users with a certain status
  57. $query = "SELECT username FROM users WHERE users.status = '{$status_list[$status]}'";
  58.  
  59. // Executes the query and stores the returned resource in $result
  60. $result = mysql_query($query);
  61.  
  62. // If $result is a resource instead of FALSE...
  63. if ($result)
  64. {
  65.     // Fetches a row until there are no more rows to fetch
  66.     while ($row = mysql_fetch_assoc($result))
  67.     {
  68.         // Stores the username from the fetched row in the $usernames array
  69.         $usernames[] = $row['username'];
  70.     }
  71. }
  72.  
  73.  
  74. //------------------------------------------------------------
  75. // Part 5: Building a List of Usernames
  76. //============================================================
  77.  
  78. // If there are some usernames in the array...
  79. if (count($usernames) > 0)
  80. {
  81.     ?>
  82.  
  83.     <ul>
  84.     <td><select name="users" maxlength="23" >
  85.         <?php
  86.         // Loops through the list of usernames
  87.         foreach ($usernames as $name )
  88.         {
  89.             // Builds a list item for each username
  90.             echo '<option>'.$name.'</option>';
  91.         }
  92.         ?>
  93.  
  94.         </select> </td>
  95.  
  96.     </ul>
  97.     <?php
  98. }
  99. else
  100. {
  101.     ?>
  102.     <td><select name="users" maxlength="23" type="multiple">
  103.                <?
  104.                echo '<option>'." ".'</option>'; ?>
  105.  
  106.  
  107.     <?php
  108. }
  109. ?>
  110.  
Apr 28 '09 #1
Share this Question
Share on Google+
45 Replies


acoder
Expert Mod 15k+
P: 16,027
There's at least two ways in which you could do this:
1. Loading all the data on page load (using PHP to generate JavaScript code) and then using JavaScript to populate the list box.
2. Using Ajax- call a PHP script which returns the options and set the select to that.

Which do you prefer?
Apr 28 '09 #2

100+
P: 116
the first one sounds like wat i mentioned
Apr 28 '09 #3

acoder
Expert Mod 15k+
P: 16,027
So, using PHP, generate something like this (assuming status type is a number):
Expand|Select|Wrap|Line Numbers
  1. var status = [];
  2. status[0] = ['joe','ann','user','and so on'];
  3. status[1] = ['some','more','users'];
then onchange (first select), empty the list box and then loop over the corresponding array to populate the contents, e.g.
Expand|Select|Wrap|Line Numbers
  1. selObj.options[i] = new Option(status[currStatus][i], status[currStatus][i]);
where currStatus is the currently selected status.
Apr 30 '09 #4

100+
P: 116
acoder i m a newbie in this and hence slightly lost when u said tht part abt onchange....btw the status is not nos. its variable like ban pending ....so what shud my code line be exactly ? and where shud i put it in?
May 2 '09 #5

100+
P: 116
u want me to put the onchange in select for users or select for status?
May 2 '09 #6

omerbutt
100+
P: 638
@angelicdevil
the onchange property in the select menu will let you call a javascript function which will be making an ajax call to a php script which will browse all the users from the data base regarding the status or whatever you are tying to view
did you get that or not ?
regards,
omer aslam
May 2 '09 #7

acoder
Expert Mod 15k+
P: 16,027
Based on what you've said earlier, it'd be status:
Expand|Select|Wrap|Line Numbers
  1. <select id="status" onchange="changeUsers()">
where changeUsers is the function that changes the second select (users).
May 2 '09 #8

100+
P: 116
thanx acoder but what is changeUser() function...what is tht code ?
May 2 '09 #9

acoder
Expert Mod 15k+
P: 16,027
That was just an example function name. You can name the function whatever you want.
May 2 '09 #10

100+
P: 116
no i m asking wat shud be there in tht function ... its code
May 2 '09 #11

acoder
Expert Mod 15k+
P: 16,027
As I mentioned earlier (quoted for reference) @acoder
you need to empty the list box (select) element (the users one). You can set the length of options to 0 or null. Then use a loop (e.g. for) to loop over the array for the correct status. See what you can manage from that.
May 2 '09 #12

100+
P: 116
this is wat i did still not working and giving parse error at 122

Expand|Select|Wrap|Line Numbers
  1.  
  2. <?php require_once("d:/wamp/www/php_lucky/includes/session.php"); ?>
  3. <?php require_once("d:/wamp/www/php_lucky/includes/connection.php"); ?>
  4. <?php require_once("d:/wamp/www/php_lucky/includes/functions.php"); ?>
  5. <?php require_once("d:/wamp/www/php_lucky/includes/form_functions.php"); ?>
  6.  
  7. <?php
  8. //------------------------------------------------------------
  9. // Part 1: Choosing a Status
  10. //============================================================
  11.  
  12. // Initializes a list of acceptable statuses
  13. $status_list = array();
  14.  
  15. $query_status = " SELECT status_type.status from status_type ";
  16.  
  17.        $result_status = mysql_query($query_status);
  18.         confirm_query($result_status);
  19.  
  20.  
  21.  
  22.         $status_list[]=" ";
  23.   while ($record = mysql_fetch_assoc($result_status)) {
  24.           $status_list[] = $record['status'] ;
  25.  
  26.         }
  27. $status = $_GET['status'];
  28.  
  29.  
  30.  
  31. // A form for choosing the status
  32. ?>
  33. <form method="get" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>">
  34.  
  35.     <select name="status" onchange="changeUsers()">
  36.         <?php
  37.  
  38.         // Loops through the $status_list array
  39.          foreach ($status_list as $value => $option)
  40.         {
  41.             // Sets an attribute to show the chosen status as selected
  42.             $selected = ($status == $value) ? ' selected="selected"' : '';
  43.  
  44.  
  45.             // Builds an option for each acceptable status
  46.            echo '<option value="'.$value.'"'.$selected.'>'.$option.'</option>';
  47.  
  48.         }
  49.  
  50.  
  51.  
  52.         ?>
  53.     </select>
  54.  
  55.  
  56. </form>
  57.  
  58. <?php
  59. //------------------------------------------------------------
  60. // Part 2: Using the Chosen Status to Search the Database
  61. //============================================================
  62.  
  63. // Initializes an empty array of usernames
  64. $usernames = array();
  65.  
  66. // Builds a query to get the names of users with a certain status
  67. $query = "SELECT username FROM users WHERE users.status = '{$status_list[$status]}'";
  68.  
  69. // Executes the query and stores the returned resource in $result
  70. $result = mysql_query($query);
  71.  
  72. // If $result is a resource instead of FALSE...
  73. if ($result)
  74. {
  75.     // Fetches a row until there are no more rows to fetch
  76.     while ($row = mysql_fetch_assoc($result))
  77.     {
  78.         // Stores the username from the fetched row in the $usernames array
  79.         $usernames[] = $row['username'];
  80.     }
  81. }
  82.  
  83.  
  84. //------------------------------------------------------------
  85. // Part 5: Building a List of Usernames
  86. //============================================================
  87.  
  88. // If there are some usernames in the array...
  89. if (count($usernames) > 0)
  90. {
  91.     ?>
  92.  
  93.     <ul>
  94.     <td><select name="users" maxlength="23" >
  95.         <?php
  96.         // Loops through the list of usernames
  97.         foreach ($usernames as $name )
  98.         {
  99.             // Builds a list item for each username
  100.             echo '<option>'.$name.'</option>';
  101.         }
  102.         ?>
  103.  
  104.         </select> </td>
  105.  
  106.     </ul>
  107.     <?php
  108. }
  109. else
  110. {
  111.     ?>
  112.     <td><select name="users" maxlength="23" type="multiple">
  113.                <?
  114.                echo '<option>'." ".'</option>'; ?>
  115.  
  116.  
  117. }
  118.  
  119. <? 
  120. function changeUsers(){
  121. selObj.options[i] = new Option(status[currStatus][i], status[currStatus][i]);
  122. }
  123. ?>
  124.  
  125.  
May 3 '09 #13

acoder
Expert Mod 15k+
P: 16,027
That's because you haven't defined selObj and the status array, etc.

Before you do sort that out, you need to define a data structure to hold the data. An array would be easiest for you. Use PHP to generate it, e.g.
Expand|Select|Wrap|Line Numbers
  1. var status = [];
  2. // generated by PHP
  3. status["astatus"] = ["some","user","names"];
May 3 '09 #14

acoder
Expert Mod 15k+
P: 16,027
Just to add (to make things easier), the steps that you need to take to get a working solution:
1. Set up a data structure containing all the data (see previous post - an array should work well).
2. Define a function to be called onchange - done.
3. In that function, access the users list box and remove all options.
4. Then get the selected option value from the status list box.
5. Use that to get the corresponding array (values) in the status array.
6. Finally, use a loop to loop over the array and add options one by one (the line you have in changeUsers currently).
May 3 '09 #15

100+
P: 116
but i need the users list to populate other fields so wont i need the option value for users?

also the status has been defined in the status_list ...its being populated with data from database
May 3 '09 #16

acoder
Expert Mod 15k+
P: 16,027
...which is why you populate it again with the new options (point 6).
May 3 '09 #17

100+
P: 116
i m feeling lost ...guess i'll try again.
May 3 '09 #18

acoder
Expert Mod 15k+
P: 16,027
@angelicdevil
But that's a PHP variable - JavaScript can't see it. You need to generate JavaScript using PHP code, e.g.
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. var status = [];
  3. <?php
  4.   while (...) {
  5.       echo "status['".$status[$i]."'] = [...];";
  6.   }
  7. ?>
May 3 '09 #19

acoder
Expert Mod 15k+
P: 16,027
@angelicdevil
Which part do you not understand?
May 3 '09 #20

100+
P: 116
u mean the select options for users and status should be populated in javascript itself?
May 3 '09 #21

100+
P: 116
ok this is wat i did still not orking....can u make the changes and post it....i cant seem to figure it out.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. // Choosing a Status
  3.     $status_list = array();
  4.  
  5.     $query_status = " SELECT status_type.status from status_type ";
  6.  
  7.         $result_status = mysql_query($query_status);
  8.         confirm_query($result_status);
  9.  
  10.  
  11.         $status_list[]=" ";
  12.         while ($record = mysql_fetch_assoc($result_status)) {
  13.         $status_list[] = $record['status'] ;
  14.  
  15.         }
  16. $status = $_GET['status'];
  17.  
  18. ?>
  19.  
  20. <form method="get" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>">
  21.  
  22.     <select name="status" onchange="changeUsers()">
  23. <script type="text/javascript">
  24. <?php
  25.  
  26. // displaying status
  27. foreach ($status_list as $value => $option)
  28.         {
  29. $selected = ($status == $value) ? ' selected="selected"' : '';
  30.  echo '<option value="'.$value.'"'.$selected.'>'.$option.'</option>';
  31.  }
  32.  
  33. ?>
  34.   </select>
  35.  
  36. </form>
  37.  
  38. <?php
  39. // Part 2: Using the Chosen Status to Search the Database
  40. $usernames = array();
  41. $query = "SELECT username FROM users WHERE users.status = '{$status_list[$status]}'";
  42. $result = mysql_query($query);
  43. if ($result)
  44. {
  45.     // Fetches a row until there are no more rows to fetch
  46.     while ($row = mysql_fetch_assoc($result))
  47.     {
  48.         // Stores the username from the fetched row in the $usernames array
  49.         $usernames[] = $row['username'];
  50.     }
  51. }
  52. if (count($usernames) > 0)
  53. {
  54.     ?>
  55.  
  56.     <ul>
  57.     <td><select name="users" maxlength="23" >
  58.         <?php
  59.         // Loops through the list of usernames
  60.         foreach ($usernames as $name )
  61.         {
  62.             // Builds a list item for each username
  63.             echo '<option>'.$name.'</option>';
  64.         }
  65.         ?>
  66.  
  67.         </select> </td>
  68.  
  69.     </ul>
  70.     <?php
  71. }
  72. else
  73. {
  74.     ?>
  75.     <td><select name="users" maxlength="23" type="multiple">
  76.                <?
  77.                echo '<option>'." ".'</option>'; ?>
  78.  
  79.  
  80. }
  81.  
  82. <? 
  83. function changeUsers(){
  84. selObj.options[i] = new Option(status_list[currStatus][i], status_list[currStatus][i]);
  85. }
  86. ?>
  87.  
May 3 '09 #22

acoder
Expert Mod 15k+
P: 16,027
@angelicdevil
Initially, the default options would be populated using PHP and subsequent options (for users) would be populated using JavaScript.
May 3 '09 #23

acoder
Expert Mod 15k+
P: 16,027
@angelicdevil
On line 23, remove the script tag. The function changeUser needs to be within script tags. You still haven't added PHP code to generate the JavaScript array to store the data. For that, you will need a query which lists the users by status.
May 3 '09 #24

100+
P: 116
damn i m just not able to get it to work!!!!! acoder please cud u make the neccessary changes and post it so i know where the hell i m messing it up big time.

thanx in advance
May 4 '09 #25

acoder
Expert Mod 15k+
P: 16,027
Well, I could have done that in the very first reply, but that wouldn't really help. I've been trying to show you the steps, so that I can help you figure it out.

However, this calls for a simple example:
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4. var statuses = [];
  5. statuses[0] = ["some","user","names"];
  6. statuses[1] = ["some2","user2","names2"];
  7. statuses[2] = ["some3","user3","names3"];
  8. function changeUsers() {
  9.   var users = document.getElementById("users");
  10.   var status = document.getElementById("status").selectedIndex;
  11.   users.options.length = 0;
  12.   userArr = statuses[status];
  13.   for (i = 0; i < userArr.length; i++) {
  14.     users.options[i] = new Option(userArr[i],userArr[i]);
  15.   }
  16. }
  17. </script>
  18. </head>
  19. <body>
  20. <form>
  21. <select id="status" onchange="changeUsers()">
  22.  <option value="0">test</option>
  23.  <option value="1">test2</option>
  24.  <option value="2">test3</option>
  25. </select>
  26. <select id="users">
  27. </select>
  28. </form>
  29. </body>
  30. </html>
Now you just need to adapt this to your requirements.
May 4 '09 #26

100+
P: 116
ok this is wat i did but now i m getting parse error. on line 128

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2.  
  3.  
  4. $status_list = array();
  5.  
  6. $query_status = " SELECT status_type.status from status_type ";
  7.  
  8.        $result_status = mysql_query($query_status);
  9.         confirm_query($result_status);
  10.  
  11.  
  12.  
  13.         $status_list[]=" ";
  14.   while ($record = mysql_fetch_assoc($result_status)) {
  15.           $status_list[] = $record['status'] ;
  16.  
  17.         }
  18. function changeUsers() {
  19.   var users = document.getElementById("users");
  20.   var status = document.getElementById("status").selectedIndex;
  21.   users.options.length = 0;
  22.   userArr = status_list[status];
  23.   for (i = 0; i < userArr.length; i++) {
  24.     users.options[i] = new Option(userArr[i],userArr[i]);
  25.   }
  26. }
  27. </script>
  28. <body>
  29. <?php
  30. //------------------------------------------------------------
  31. // Part 1: Choosing a Status
  32. //============================================================
  33.  
  34. // Initializes a list of acceptable status_list
  35.  
  36. $status = $_GET['status'];
  37.  
  38.  
  39.  
  40. // A form for choosing the status
  41. ?>
  42. <form method="get" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>">
  43.  
  44.     <select name="status" onchange="changeUsers()">
  45.         <?php
  46.  
  47.         // Loops through the $status_list array
  48.          foreach ($status_list as $value => $option)
  49.         {
  50.             // Sets an attribute to show the chosen status as selected
  51.             $selected = ($status == $value) ? ' selected="selected"' : '';
  52.  
  53.  
  54.             // Builds an option for each acceptable status
  55.            echo '<option value="'.$value.'"'.$selected.'>'.$option.'</option>';
  56.  
  57.         }
  58.  
  59.  
  60.  
  61.         ?>
  62.     </select>
  63.  
  64.  
  65. </form>
  66.  
  67. <?php
  68. //------------------------------------------------------------
  69. // Part 2: Using the Chosen Status to Search the Database
  70. //============================================================
  71.  
  72. // Initializes an empty array of usernames
  73. $usernames = array();
  74.  
  75. // Builds a query to get the names of users with a certain status
  76. $query = "SELECT username FROM users WHERE users.status = '{$status_list[$status]}'";
  77.  
  78. // Executes the query and stores the returned resource in $result
  79. $result = mysql_query($query);
  80.  
  81. // If $result is a resource instead of FALSE...
  82. if ($result)
  83. {
  84.     // Fetches a row until there are no more rows to fetch
  85.     while ($row = mysql_fetch_assoc($result))
  86.     {
  87.         // Stores the username from the fetched row in the $usernames array
  88.         $usernames[] = $row['username'];
  89.     }
  90. }
  91.  
  92.  
  93. //------------------------------------------------------------
  94. // Part 5: Building a List of Usernames
  95. //============================================================
  96.  
  97. // If there are some usernames in the array...
  98. if (count($usernames) > 0)
  99. {
  100.     ?>
  101.  
  102.     <ul>
  103.     <td><select name="users" maxlength="23" >
  104.         <?php
  105.         // Loops through the list of usernames
  106.         foreach ($usernames as $name )
  107.         {
  108.             // Builds a list item for each username
  109.             echo '<option>'.$name.'</option>';
  110.         }
  111.         ?>
  112.  
  113.         </select> </td>
  114.  
  115.     </ul>
  116.   <?
  117. }
  118. else
  119. {
  120.     ?>
  121.     <td><select name="users" maxlength="23" type="multiple">
  122.                <?php
  123.                echo '<option>'." ".'</option>'; ?>
  124.  
  125. <?   
  126. }
  127. ?>
  128.  
  129. </body>
  130.  
May 8 '09 #27

100+
P: 116
i checked again and again cant seem o get it to work...please see if i have coded it right and can correct it too?
May 8 '09 #28

Dormilich
Expert Mod 5K+
P: 8,639
if you don't have short tags enabled, replace <? by <?php (lines 116, 125)
May 8 '09 #29

acoder
Expert Mod 15k+
P: 16,027
The code that you have from lines 4 to 17 is PHP code, but it's not within PHP tags. I think you meant to create the JavaScript arrays, but you need to echo it out onto the page. The syntax needs to be correct too. Look at the example. Did you try it out? Now the PHP code (when you check the source on the client) should produce something similar.
May 8 '09 #30

100+
P: 116
hi i found a code on net which does wat i want ...i tried to modify it to fit my needs but its not working as i want it to ....could be please check....

dd.php
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. $dbservertype='mysql';
  4. $servername='localhost';
  5. // username and password to log onto db server
  6. $dbusername='lucky';
  7. $dbpassword='lucky';
  8. // name of database
  9. $dbname='stockphotos';
  10.  
  11.  
  12. ////// DO NOT EDIT BELOW  /////////
  13.  
  14. connecttodb($servername,$dbname,$dbusername,$dbpassword);
  15. function connecttodb($servername,$dbname,$dbuser,$dbpassword)
  16. {
  17. global $link;
  18. $link=mysql_connect ("$servername","$dbuser","$dbpassword");
  19. if(!$link){die("Could not connect to MySQL");}
  20. mysql_select_db("$dbname",$link) or die ("could not open db".mysql_error());
  21. }
  22. //////// End of connecting to database ////////
  23. ?>
  24.  
  25.  
  26.  
  27. <html>
  28.  
  29. <head>
  30. <title>Multiple drop down list box </title>
  31. <SCRIPT language=JavaScript>
  32. function reload(form)
  33. {
  34. var val=form.cat.options[form.cat.options.selectedIndex].value;
  35. self.location='dd.php?cat=' + val ;
  36. }
  37.  
  38. </script>
  39. </head>
  40.  
  41. <body>
  42. <?
  43.  
  44. @$cat=$_GET['cat']; // Use this line or below line if register_global is off
  45. if(!is_numeric($cat)){ // to check if $cat is numeric data or not. 
  46. echo "Data Error";
  47. exit;
  48. }
  49.  
  50. // Getting the data from Mysql table for first list box//////////
  51. $query_usernamey_status=mysql_query("SELECT status,status_name FROM status_type "); 
  52. // End of query for first list box////////////
  53.  
  54. // for second drop down list we will check if category is selected else we will display all the subcategory///// 
  55. if(isset($cat) and strlen($cat) > 0){
  56. $query_username=mysql_query("SELECT username FROM users WHERE status=$cat "); 
  57. // end of query for second subcategory drop down list box ///////////////////////////
  58.  
  59. echo "<form method=post name=f1 action='dd-check.php'>";
  60. // Add your form processing page address to action in above line. Example  action=dd-check.php////
  61.  
  62. //        Starting of first drop downlist /////////
  63. echo "<select name='cat' onchange=\"reload(this.form)\"><option value=''>Select one</option>";
  64. while($record1 = mysql_fetch_array($query_usernamey_status)) { 
  65. if($record1['status']==@$cat){echo "<option selected value='$record1[status]'>$record1[status_name]</option>"."<BR>";}
  66. else{echo  "<option value='$record1[status]'>$record1[status_name]</option>";}
  67. }
  68. echo "</select>";
  69. //  This will end the first drop down list ///////////
  70.  
  71.  
  72. //        Starting of second drop downlist /////////
  73. echo "<select name='subcat'><option value=''>Select one</option>";
  74. while($record2 = mysql_fetch_array($query_username)) { 
  75. echo  "<option value='$record2[username]'>$record2[username]</option>";
  76. }
  77. echo "</select>";
  78. //  This will end the second drop down list ///////////
  79.  
  80. // Add your other form fields as needed here/////
  81. echo "<input type=submit value=Submit>";
  82. echo "</form>";
  83. ?>
  84. <center></center> 
  85. </body>
  86.  
  87. </html>
  88.  
dd-check.php
Expand|Select|Wrap|Line Numbers
  1.  
  2. <html>
  3.  
  4. <head>
  5. <title> Multiple drop down list box </title>
  6. <meta name="GENERATOR" content="Arachnophilia 4.0">
  7. <meta name="FORMATTER" content="Arachnophilia 4.0">
  8. </head>
  9.  
  10. <body>
  11. <?
  12. $cat=$_POST['cat'];
  13. $subcat=$_POST['subcat'];
  14.  
  15. echo "Value of \$cat = $cat <br>Value of \$subcat = $subcat ";
  16.  
  17.  
  18. ?>
  19. <center>
  20.  
  21. </center> 
  22. </body>
  23.  
  24. </html>
  25.  
plz help
May 12 '09 #31

acoder
Expert Mod 15k+
P: 16,027
The line:
Expand|Select|Wrap|Line Numbers
  1. self.location='dd.php?cat=' + val ;
reloads the page to change the options. I thought you wanted to avoid that. With the code you've posted, the whole approach changes which means you're effectively back to square one.
May 12 '09 #32

100+
P: 116
i m fine with it as long as it loads the values based on selection ...
May 12 '09 #33

100+
P: 116
can u figure out wats going wrong in this new one?
May 12 '09 #34

acoder
Expert Mod 15k+
P: 16,027
The problem from a cursory glance seems to be in your PHP code, e.g. $record1[status] should be $record['status']. Do you not see any errors?
May 12 '09 #35

100+
P: 116
yes i m getting the error below

Expand|Select|Wrap|Line Numbers
  1. 0){ $quer=mysql_query("SELECT username FROM users WHERE status=$cat "); } // end of query for second subcategory drop down list box /////////////////////////// echo "
  2. "; // Add your form processing page address to action in above line. Example action=dd-check.php//// // Starting of first drop downlist ///////// echo ""; // This will end the first drop down list /////////// // Starting of second drop downlist ///////// echo ""; // This will end the second drop down list /////////// // Add your other form fields as needed here///// echo ""; echo "
  3. "; ?>
May 12 '09 #36

100+
P: 116
with record or record1 the error is same
May 12 '09 #37

acoder
Expert Mod 15k+
P: 16,027
That's not an error message: it's code. Maybe the problem is that you're using short tags <? instead of the full PHP opening tag <?php on line 42.
May 12 '09 #38

100+
P: 116
changed it to full tags still same issue
May 12 '09 #39

acoder
Expert Mod 15k+
P: 16,027
I suggest you post a new thread in the PHP forum because the problem relates to PHP and is no longer relevant to this thread.
May 12 '09 #40

100+
P: 116
wat abt the previous script where i modified as u said.....any thing can be done to make tht work?
May 13 '09 #41

acoder
Expert Mod 15k+
P: 16,027
Yes, of course. Did you make the changes as suggested in posts #29/30?
May 13 '09 #42

100+
P: 116
yes still they r not working
May 13 '09 #43

acoder
Expert Mod 15k+
P: 16,027
Let me see the latest version, so we know what we're working off.
May 13 '09 #44

100+
P: 116
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. var statuses = [];
  3. $status_list = array();
  4. <?php
  5. $query_status = " SELECT status_type.status from status_type ";
  6.  
  7.        $result_status = mysql_query($query_status);
  8.         confirm_query($result_status);
  9.  
  10.  
  11.  
  12.         $status_list[]=" ";
  13.   while ($record = mysql_fetch_assoc($result_status)) {
  14.           $status_list[] = $record['status'] ;
  15.  
  16.         }
  17. ?>
  18. function changeUsers() {
  19.   var users = document.getElementById("users");
  20.   var status = document.getElementById("status").selectedIndex;
  21.   users.options.length = 0;
  22.   userArr = statuses[status];
  23.   for (i = 0; i < userArr.length; i++) {
  24.     users.options[i] = new Option(userArr[i],userArr[i]);
  25.   }
  26. }
  27. </script>
  28. <?php
  29. //------------------------------------------------------------
  30. // Part 1: Choosing a Status
  31. //============================================================
  32.  
  33. // Initializes a list of acceptable statuses
  34.  
  35. $status = $_GET['status'];
  36.  
  37.  
  38.  
  39. // A form for choosing the status
  40. ?>
  41. <form method="get" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>">
  42.  
  43.     <select name="status" onchange="changeUsers()">
  44.         <?php
  45.  
  46.         // Loops through the $status_list array
  47.          foreach ($status_list as $value => $option)
  48.         {
  49.             // Sets an attribute to show the chosen status as selected
  50.             $selected = ($status == $value) ? ' selected="selected"' : '';
  51.  
  52.  
  53.             // Builds an option for each acceptable status
  54.            echo '<option value="'.$value.'"'.$selected.'>'.$option.'</option>';
  55.  
  56.         }
  57.  
  58.  
  59.  
  60.         ?>
  61.     </select>
  62.  
  63.  
  64. </form>
  65.  
  66. <?php
  67. //------------------------------------------------------------
  68. // Part 2: Using the Chosen Status to Search the Database
  69. //============================================================
  70.  
  71. // Initializes an empty array of usernames
  72. $usernames = array();
  73.  
  74. // Builds a query to get the names of users with a certain status
  75. $query = "SELECT username FROM users WHERE users.status = '{$status_list[$status]}'";
  76.  
  77. // Executes the query and stores the returned resource in $result
  78. $result = mysql_query($query);
  79.  
  80. // If $result is a resource instead of FALSE...
  81. if ($result)
  82. {
  83.     // Fetches a row until there are no more rows to fetch
  84.     while ($row = mysql_fetch_assoc($result))
  85.     {
  86.         // Stores the username from the fetched row in the $usernames array
  87.         $usernames[] = $row['username'];
  88.     }
  89. }
  90.  
  91.  
  92. //------------------------------------------------------------
  93. // Part 5: Building a List of Usernames
  94. //============================================================
  95.  
  96. // If there are some usernames in the array...
  97. if (count($usernames) > 0)
  98. {
  99.     ?>
  100.  
  101.     <ul>
  102.     <td><select name="users" maxlength="23" >
  103.         <?php
  104.         // Loops through the list of usernames
  105.         foreach ($usernames as $name )
  106.         {
  107.             // Builds a list item for each username
  108.             echo '<option>'.$name.'</option>';
  109.         }
  110.         ?>
  111.  
  112.         </select> </td>
  113.  
  114.     </ul>
  115.  
  116. }
  117. else
  118. {
  119.  
  120.     <td><select name="users" maxlength="23" type="multiple">
  121.                <?
  122.                echo '<option>'." ".'</option>'; ?>
  123.  
  124.  
  125. }
  126.  
  127.  
May 13 '09 #45

acoder
Expert Mod 15k+
P: 16,027
Put line 3 inside the PHP tags. Your PHP code at the beginning doesn't actually produce any output. It needs to echo out some data. For that, you will need a second query which gets the user names based on each status (see original PHP code). As an example:
Expand|Select|Wrap|Line Numbers
  1. while ($record = mysql_fetch_assoc($result_status)) {
  2.     $status_list[] = $record['status'] ;
  3.     $query = "SELECT username FROM users WHERE users.status = '{$record['status']}'";
  4.     $result = mysql_query($query);
  5.     echo "statuses[$i] = [";
  6.     while ($row = mysql_fetch_assoc($result))
  7.     {
  8.         echo $row['username'].",";
  9.     }
  10.     echo "];";
  11. }
I haven't tested this code, but based it on code you already have just to give you an idea.
May 13 '09 #46

Post your reply

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