473,394 Members | 2,063 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,394 software developers and data experts.

dynamic populating of fields based on selection

116 100+
ok this is wat i was trying to do...i have a list field with options A,B,P now i want the second list field to show data list by getting them from database based on the selection made in listbox1.

e.g if i select A in listbox 1 then listbox 2 should display automatically all the data from database where the status is A.
and as i change the seelction it should display different data accordingly.

but in my code it doesnt display any data in the listbox2

plz help

Expand|Select|Wrap|Line Numbers
  1.  
  2. <body>
  3.  
  4.  
  5. <div style="position: absolute; width: 100px; height: 32px; z-index: 1; left: 225px; top: 5px" id="layer1">
  6. <td><select name="status" maxlength="1">
  7. <?php
  8.  
  9.             echo '<option>'.A.'</option>';echo "<BR>";
  10.              echo '<option>'.P.'</option>';echo "<BR>";
  11.               echo '<option>'.B.'</option>';echo "<BR>";
  12.  
  13. ?> 
  14.  
  15.  
  16.   </select> </td>
  17.   </div>
  18. <form action="listpopulate.php" method="post">
  19. <table>
  20.  
  21. <td><select name="username" maxlength="23">
  22. <?php
  23.            $username = 'username';
  24.     $usernamevalue = array();
  25.     $status = 'status';      
  26.  
  27.     $query_name = "SELECT users.username FROM users WHERE users.status='".mysql_real_escape_string($status)."'";
  28.     $result = mysql_query($query_name);
  29.     confirm_query($result);
  30.             while ($record = mysql_fetch_assoc($result)) {
  31.         while (list($username, $usernamevalue) = each ($record)) {
  32.         echo '<option>'.htmlentities($usernamevalue).'</option>';
  33.  
  34.         }
  35.         echo "<BR>";
  36.  
  37.     }   
  38.  
  39.  
  40. ?> 
  41.   </select> </td>
  42.  
  43.  
  44. </div>
  45.  
  46.  
  47.  </table>
  48.  
  49. </form>
  50. </body>
  51.  
  52.  
  53.  
Apr 26 '09 #1
11 2286
Markus
6,050 Expert 4TB
Your option elements need a value attribute. That way, when you submit the form, you will know the value of the selected option in the $_POST array.

Form:
Expand|Select|Wrap|Line Numbers
  1. <form ... >
  2.   <select name="status">
  3.     <option value="1">1</option>
  4.     <option value="2">2</option>
  5.     <option value="3">3</option>
  6.   </select>
  7. </form>
  8.  
And your PHP would look something like below:
Expand|Select|Wrap|Line Numbers
  1. // If there is no POST data present, we'll set a default value.
  2. // Read: ternary operator (php.net)
  3. $status = (isset($_POST['status'])) ? $_POST['status'] : 1;
  4.  
  5. echo "You selected: {$status}";
  6.  
  7. // Use $status in SQL.
  8. $sql = "SELECT * FROM `tbl_name` WHERE `status` = '{$status'}";
  9.  
Hope this helps,
Mark.
Apr 27 '09 #2
angelicdevil
116 100+
ok this is what i changed as you said but its not auto populating based on selection

plz tell me what i need to change on which line nos.

thanx

Expand|Select|Wrap|Line Numbers
  1. <body>
  2.  
  3. <form action="listpopulate.php" method="post">
  4. <table>
  5. <td><select name="status" maxlength="1">
  6. <?php
  7.        $status_name = 'status';
  8.        $status_value = array();
  9.        $query_status = " SELECT status_type.status from status_type ";
  10.        $result_status = mysql_query($query_status);
  11.         confirm_query($result_status);
  12.  
  13.        // status list box // 
  14.  
  15.        while ($record = mysql_fetch_assoc($result_status)) {
  16.         while (list($status_name, $status_value) = each ($record)) {
  17.         echo '<option value ="'.htmlentities($status_value).'">'.htmlentities($status_value).'</option>';
  18.  
  19.         }
  20.          }
  21.          $status = (isset($_POST['status'])) ? $_POST['status'] : "";
  22.          if ($status == $_POST['status']){
  23.          $c = 1;
  24.          }
  25.  
  26.  
  27. ?> 
  28.  
  29.   </select> </td>
  30.  
  31.  
  32. <td><select name="username" maxlength="23">
  33. <?php
  34.  
  35.            $username = 'username';
  36.     $usernamevalue = array();
  37.  
  38.     $query_name = "SELECT users.username FROM users WHERE users.status='{$status}'";
  39.         $result = mysql_query($query_name);
  40.         confirm_query($result);
  41.     // users listbox // 
  42.     if ($c ==1){
  43.     if ($result)
  44. {
  45.         while ($record = mysql_fetch_assoc($result)) {
  46.         while (list($username, $usernamevalue) = each ($record)) {
  47.         echo '<option value ="'.htmlentities($usernamevalue).'">'.htmlentities($usernamevalue).'</option>';
  48.  
  49.         }
  50.         echo "<BR>";
  51.  
  52.     }   
  53.  
  54.  } 
  55.  
  56.  } 
  57. ?> 
  58.   </select> </td>
  59.    </table>
  60.  
  61. </form>
  62. </body> 
  63.  
Apr 28 '09 #3
waqasahmed996
160 100+
i think you have to apply ajax here

by using ajax you can easily do this
Apr 28 '09 #4
Markus
6,050 Expert 4TB
Lines 22 - 24 make no sense. The reason for using the ternary operator is to easily set a default value based on a condition.

Expand|Select|Wrap|Line Numbers
  1. $status = (isset($_POST['status'])) ? $_POST['status'] : 1;
  2.  
Are you wanting the 'auto populating' to be done without the page refreshing?
Apr 28 '09 #5
angelicdevil
116 100+
yes without page refreshing or click button
Apr 28 '09 #6
Markus
6,050 Expert 4TB
Then you need to look into, AJAX. There are lots of readily available tutorials if you search google.
Apr 28 '09 #7
angelicdevil
116 100+
something like http://www.plus2net.com/php_tutorial/dd.php only they wat their r doing is going over my head . it wud be gr8 if u can help me get same result with my code or how i can make changes in tht code to get my result. since they use javascript n i know nothing abt java
Apr 28 '09 #8
Dormilich
8,658 Expert Mod 8TB
@angelicdevil
Java and Javascript have only the first four letters in common ;). besides that, Javascript is an essential part of AJAX (thus the name).

if the javascript is too much for you consider using a Javascript library (like ExtJS, JQuery, Prototype, …) they should provide functions to make AJAX actions easier.
Apr 28 '09 #9
Ciary
247 Expert 100+
one hing to know about PHP: it's only execued at page load. so if you want do do anything with PHP you either need to reload the page or use an AJAX request to a php page with the code.

if you really want something to change without reloading the page u need to use javascript. with php this can't be done.

a bit of theory: php is a server side language which wil be executed on the server before your browser receives it. thats why you can't see php in your sourcecode. and you just cant execute php without asking the server to reload te page.

i hope that helps you understand why it must be done using ajax/javascript

@Dormilich
Asynchronous Javascript And Xml = AJAX
Apr 28 '09 #10
angelicdevil
116 100+
jesus i m stuck then...does anyone of u know javascript ?
Apr 28 '09 #11
Markus
6,050 Expert 4TB
Head to the Javascript forum, and ask there for help.
Apr 28 '09 #12

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

Similar topics

0
by: B | last post by:
Using Access2000, the sample code below is what I have been modifying and working on since the past week and I could not get it to work properly. What I wanted to accomplish: 1) read from a...
2
by: John Ninan | last post by:
I am creating Dynamic Usercontrol in Asp.net application. In this application I have a combobox(aspx Page). Which contains various items. Based on item selected I am dynamically populating...
0
by: pbb | last post by:
I have a web page on which I dynamically create controls based on the selection a user makes from a dropdownlist (this ddl is not dynamic). Depending on the user's selection, the controls could be...
4
by: Larry Grady | last post by:
Anyone up for a challenge? I've been struggling with this for a few days and was hoping someone could help me. Pouring through all the messageboards I just can't find the solution. We have a...
0
by: Pascal Costanza | last post by:
Dynamic Languages Day @ Vrije Universiteit Brussel ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Monday, February 13, 2006, VUB Campus Etterbeek The VUB (Programming Technology Lab,...
19
by: Alex | last post by:
Hello list This question has probably already been asked, but let me ask again I have a mysql database to which I connect with my php scripts. The database contains articles. Name, Unit_Price...
1
by: emmaruwa | last post by:
I have a form with two text boxes (in its details section) that pull data from two fields in my database table. This same form also has a button beside the text boxes which is supposed to open...
1
by: jmartmem | last post by:
Greetings, I have a nagging problem with client-side dynamic dependent list boxes that perhaps someone can help me troubleshoot. I have a form with a series of dynamic dependent list boxes....
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.