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

PHP listbox

121 100+
hi

I have two listboxes, if i select an item from first listbox relative items(fetching from database) of selected item should be loaded into second listbox. how we can solve this problem in php.

one thing i already got the above result using php + javascript.
i need the code which is written purely in PHP without using javascript, is it possible?

regards,
Shiva
Aug 24 '07 #1
1 7557
Atli
5,058 Expert 4TB
Yes, it is indeed possible.
You can simply populate your first list-box in any way you see fit and instruct it, via the 'onChange' event to submit the form. The form's action property would point to the very page it is on, causing the page to refresh and the data to be sent along for the ride.
The you can simply have you PHP code check if the first box has a value and populate your second box based on the value of the first box.

This example is quite static, but shows what I mean. You will of course have to replace the huge array of data with data from whatever source you get your data from and modify the foreach() loops accordingly.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. // Get the values of the list-boxes
  3. $firstBox = @$_POST['firstBox'];
  4. if(empty($firstBox)) 
  5.     $firstBox = "first";
  6. $secondBox = @$_POST['secondBox'];
  7. if(empty($secondBox)) 
  8.     $secondBox = "first";
  9.  
  10. // Print the form header
  11. echo '<form action="?" method="POST">';
  12.  
  13. // Crete the data for the boxes
  14. // Note, this will most likely be
  15. // replace by your date, however you get it.
  16. $boxData = array(
  17.     "first" => array(
  18.         "Title" => "First",
  19.         "Value" => "first",
  20.         "Data" => array(
  21.             "first" => "First of the first",
  22.             "second" => "Second of the first",
  23.             "third" => "Third of the first"
  24.         )
  25.     ),
  26.     "second" => array(
  27.         "Title" => "Second",
  28.         "Value" => "second",
  29.         "Data" => array(
  30.             "first" => "First of the second",
  31.             "second" => "Second of the second",
  32.             "third" => "Third of the second"
  33.         )
  34.     )
  35. );
  36.  
  37. // Print the first box
  38. echo '<select name="firstBox" onChange="submit();">';
  39. foreach($boxData as $v) {
  40.     $selected = "";
  41.     if($v['Value']  == $firstBox) $selected = " selected";
  42.     echo '    <option value="'. $v['Value'] .'"'. $selected .'>'. $v['Title'] .'</option>';
  43. }
  44. echo '</select>';
  45.  
  46. // Print the second box
  47. echo '<select name="secondBox" onChange="submit();">';
  48. foreach($boxData[$firstBox]['Data'] as $k => $v) {
  49.     $selected = "";
  50.     if($k  == $secondBox) $selected = " selected";
  51.     echo '    <option value="'. $k .'"'. $selected .'>'. $v .'</option>';
  52. }
  53. echo '</select>';
  54.  
  55.  
  56. // Close the form
  57. echo '</form>';
  58.  
  59. // Show the selected boxes
  60. echo "<p>You have selected: $firstBox > $secondBox </p>";
  61. ?>
  62.  
Aug 26 '07 #2

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

Similar topics

17
by: amber | last post by:
Hello. Can someone tell me what I may be doing wrong here? I'm using the code (lboxRP is a listbox): Dim newRPindex As Integer newRPindex = Me.lboxRP.FindString(RP)...
3
by: Paul T. Rong | last post by:
I have a listbox (of product names) control on my form. I want to pass the selected item (a product name) to a subform, and the product unitprice should apear automatically next to the product name...
8
by: Oddball | last post by:
Ok - I have a ListBox control and I'm ready to write my own DrawItem event handler. What I want to draw as the item is another control. I have created a user control that I would like to list in...
2
by: collie | last post by:
Hi, I have 2 listboxes. The first gets populated from the db as soon as the page loads. The second listbox get populated based on the user's selection from the first listbox. However,...
6
by: Chris Leuty | last post by:
I am populating a multiselect Listbox from a dataset, with the content of the listbox filled by one table, and the selections determined from another table. So far, I have been keeping the dataset...
1
by: yamne | last post by:
I have a problem. When I click in edit datagrid button I show two listbox and two button. I use two button to move data between two listbox. My problem is that I can't call the listbox in the...
7
by: Dave | last post by:
Hi all, After unsuccessfully trying to make my own dual listbox control out of arraylists, I decided to look for a 3rd party control. I've looked for over a week now and can't find anything but...
3
by: Ali Chambers | last post by:
Hi, I have created a listbox called "dtlist1" on my VB.NET form. I call a procedure as follows: Private Sub openfile(flname As String) dtlist1.Items.Clear() etc..
1
by: Sunray | last post by:
I have a form called the sales form and i have 2 sets of listboxes So what happens is. i add items form the bottom set of list boxes which are bound to a data base to the top set of list boxes which...
5
by: Academia | last post by:
(If you've seen this in the drawing NG, sorry. I inadvertently sent it there.) I have a listbox populated with Objects. The Class has a String field that ToString returns. I assume that...
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: 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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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
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.