I'm using two drop down list ina form. I have generated the first drop down list from MySQL database. When i select an option from first drop down list, i have to generate second drop down list options by the selected value of first drop down list from MySQL database.
For example, the first drop down list contains Animals, Birds... If i select the Animal option, the second drop down list should show like Lion, Tiger....
Both lists should be geneted from database only. Any script available for this?
What I have to do? Any one can help me?!
5 18692
YOU ARE URGED TO USE THE code OR php TAGS WHEN POSTING CODE!!!!
[php]
<?php
mysql_connect("hostname","username","password");
mysql_select_db("databasename");
$firstQry = mysql_query("select * from table1") or die(mysql_error()); // table 1
//////After getting the id from the table1 supply that posted id to get the resuls from the table2 //////////////
$secondQry = mysql_query("select * from table2 where edu_id = '".$_POST["first"]."'") or die(mysql_error()); // table 2
?>
<form name="check" method="post">
<!-- First DropDown starts here -->
<select name="first" onchange="this.form.submit()">
<?php while($res = mysql_fetch_array($firstQry)){?>
<option value="<?php echo $res["id"]?>"><?php echo $res["fullname"]?></option>
<?php }unset($firstQry,$res);?>
</select>
<!-- First DropDown ends here -->
<!-- Second DropDown starts here -->
<?php if(isset($_POST["first"])){?>
<select name="second">
<?php while($row = mysql_fetch_array($secondQry)){?>
<option value="<?php echo $row["edu_id "]?>"><?php echo $row["edu_qualification"]?></option>
<?php }
}unset($secondQry,$row);?>
</select>
<!-- Second DropDown ends here -->
</form>
[/php]
YOU ARE URGED TO USE THE code OR php TAGS WHEN POSTING CODE!!!!
I've used your code
I'm having some problems with submiting in the first query.. I mean when I submit in the first query and i ask later for value it gives the next submited value in first query and the previous chosen value of second query
how can I use the same code but without submiting inside my first query ?
maybe I need to use Java Script ??
the best way to do this is to use AJAX - there is a similar post in the forums relating to this. I have used this successfully for a number of drop down boxes for exactly the same situation.
you need an html page, a js page and the php page.
in your html drop box one you will need an onchange function linking it to the javascript file.
in the javascript, you should include the xmlhttp request to the php page that queries the database for the drop down list filtered, for example, a URL variable generated from the javascript. The php should have an echo function, and this can be used to populate your second drop box using getelementbyid.innerhtml in the javascript page
the tutorial at w3schools.com for AJAX should get you going on the javascript syntax you will need, especially the one relating to AJAX and database interactions...
the best way to do this is to use AJAX - there is a similar post in the forums relating to this. I have used this successfully for a number of drop down boxes for exactly the same situation.
you need an html page, a js page and the php page.
in your html drop box one you will need an onchange function linking it to the javascript file.
in the javascript, you should include the xmlhttp request to the php page that queries the database for the drop down list filtered, for example, a URL variable generated from the javascript. The php should have an echo function, and this can be used to populate your second drop box using getelementbyid.innerhtml in the javascript page
the tutorial at w3schools.com for AJAX should get you going on the javascript syntax you will need, especially the one relating to AJAX and database interactions...
Try this site.... http://roshanbh.com.np/2008/01/popul...x-and-php.html
I am using this code and am able to get it to work with my database for the first two option. I have soccer data, and the first dropdown I select the league, then based on that, the second dropdown populates with teams based on what league they are in; this is to select the home team. I am trying to add a 3rd dropdown which also populates with teams based on the league, which would be the away team.
I have two issues:
1.) The 3rd dropdown shows up after I select from the 2nd dropdown, but is not populated
2.) When I do the last "submit", I want to have it spit out some information based on the teams selected. I have the original script running perfectly, but of course the dropdowns are not populated dynamically; they list *all* the teams, and I press the "Submit" button only once. How would I tell the script after I choose from the 3rd dropdown to actually submit the data and spit out my results?
Here is my code as-is (just the three dropdowns; nothing extra yet): -
-
?php
-
require_once("../_includes/db_con.inc.php");
-
$database="soccerData";
-
$firstQry = mysql_query("SELECT DISTINCT League FROM $database ORDER BY League") or die(mysql_error());
-
$secondQry = mysql_query("SELECT DISTINCT HomeTeam FROM $database WHERE League = '".$_POST["first"]."'") or die(mysql_error());
-
$thirdQry = mysql_query("SELECT DISTINCT AwayTeam FROM $database WHERE League = '".$_POST["first"]."'") or die(mysql_error());
-
?>
-
-
<form name="check" method="post">
-
<!-- First DropDown starts here -->
-
<select name="first" onchange="this.form.submit()">
-
<?php
-
echo "<option value=\"null\">Select League</option>";
-
while($row1 = mysql_fetch_array($firstQry)) { ?>
-
<option value="<?php echo $row1['League'] ?>"><?php echo $row1['League'] ?></option>
-
<?php } unset($firstQry,$row1); ?>
-
</select>
-
<!-- First DropDown ends here -->
-
<!-- Second DropDown starts here -->
-
<?php if(isset($_POST['first'])) { ?>
-
<select name="second" onchange="this.form.submit()">
-
<?php
-
echo "<option value=\"null\">Select Home Team</option>";
-
while($row2 = mysql_fetch_array($secondQry)) { ?>
-
<option value="<?php echo $row2['HomeTeam'] ?>"><?php echo $row2['HomeTeam'] ?></option>
-
<?php }
-
} unset($secondQry,$row2); ?>
-
</select>
-
<!-- Second DropDown ends here -->
-
-
!-- Third DropDown starts here -->
-
<?php if(isset($_POST['second'])) { ?>
-
<select name="third">
-
<?php
-
echo "<option value=\"null\">Select Away Team</option>";
-
while($row3 = mysql_fetch_array($thirdQry)) { ?>
-
<option value="<?php echo $row3['AwayTeam'] ?>"><?php echo $row3['AwayTeam'] ?></option>
-
<?php }
-
} unset($thirdQry,$row3); ?>
-
</select>
-
<!-- Third DropDown ends here -->
-
<input type="submit" value="submit" name="submit">
-
</form>
-
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Dec |
last post by:
Ok to simplify things I'll just give an example.
This is pretty much what I want to do (minus the postcode):
http://www.perrys.co.uk/usedcar?ID=F5J9BNNBMVK00DF
I have relatively little...
|
by: Dec |
last post by:
Ok to simplify things I'll just give an example.
This is pretty much what I want to do (minus the postcode):
http://www.perrys.co.uk/usedcar?ID=F5J9BNNBMVK00DF
I have relatively little...
|
by: SamSpade |
last post by:
There seems to be two ways to put things on the clipboard ( I don't mean
different formats): SetClipboardData and OleSetClipboard
If I want to get data off the clipboard do I care how it was put...
|
by: Ajay Krishnan Thampi |
last post by:
I have a slight problem implementing 'drag and drop' from a datagrid
to a tree-view. I have pasted my code below. Someone please advice me
on what to do...pretty blur right now.
==code==
...
|
by: Don Wash |
last post by:
Hi There!
I have a Server-side Drop-down box in ASP.NET (VB) page. What do I do to
widen the Drop down box's Pull-Down list's width?
I'm not talking about the Drop-down box's width but the box...
|
by: lorirobn |
last post by:
Hi,
I have a form with several combo boxes, continuous form format, with
record source a query off an Item Table. The fields are Category,
Subcategory, and Color. I am displaying descriptions,...
|
by: ashok893 |
last post by:
I'm using two drop down list in a form. I have generated the first drop down list from MySQL database with the help of PHP. When i select an option from first drop down list, i have to generate...
|
by: RAM |
last post by:
I need to have two drop down lists in a edited row of a data list. One drop
down list should contain Groups and the second one should contain Materials
from *selected* group.
Thus, I have written:...
|
by: inbamca |
last post by:
Hi,
Functionality is like when pressing 'CTRL+click' on drop down list in the main jsp page, a popup window appears with a list box contains all the values of the drop down clicked on the main page....
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
| |