include "config.php"; // Database connection using PDO
//$sql="SELECT name,id FROM student";
$sql="SELECT name,id FROM student order by name";
/* You can add order by clause to the sql statement if the names are to be displayed in alphabetical order */
echo "<select name=student value=''>Student Name</option>"; // list box select command
foreach ($dbo->query($sql) as $row){//Array or records stored in $row
echo "<option value=$row[id]>$row[name]</option>";
/* Option values are added by looping through the array */
}
echo "</select>";// Closing of list box
$sql="SELECT name,id FROM student WHERE sex='male' order by name ";
Or we can display only female
$sql="SELECT name,id FROM student WHERE sex='female' order by name";
foreach ($dbo->query($sql) as $row){//Array or records stored in $row
if($row[id]==10){
echo "<option value=$row[id] selected>$row[name]</option>";
}else{
echo "<option value=$row[id]>$row[name]</option>";
}
}
echo "</select>";// Closing of list box
In this code we are populating the listbox one time while the page is loading. So this is not conditional listing. This loading can be changed so listbox will populate based on some user action.
require "config.php";// Database connection
//////////////////////////////
if($r_set = $connection->query("SELECT * from student")){
echo "<select id=name name=name class='form-control' style='width:100px;'>";
while ($row = $r_set->fetch_assoc()) {
echo "<option value=$row[id]>$row[name]</option>";
}
echo "</select>";
}else{
echo $connection->error;
}
<?Php
require "config.php";// database connection
$sql="SELECT name,id FROM student";
foreach ($dbo->query($sql) as $row) {
echo "$row[id] , $row[name]<br>";
}
?>
yogesh | 17-06-2014 |
I want to retrieve data from database to multiple select dropdown list using PHP.so that the user can select multiple items in that dropdown.Can any one help me. Any help is appreciated. Thanks. |
Dee | 15-07-2014 |
Hi, Thank you for the post. I have tried the code but its not working, The select list is blank. I am trying to come up with a code to select postal codes from the database. Below is the code; <?php include "connection.php"; // Database connection using PDO //$sql="SELECT name,id FROM student"; $sql="SELECT 'office_name','office_code' FROM postalcodes"; /* You can add order by clause to the sql statement if the names are to be displayed in alphabetical order */ echo "<select name=office_name value=''>office_name</option>"; // list box select command foreach ($dbo->query($sql) as $row){//Array or records stored in $row echo "<option value=$row[office_code]>$row[office_name]</option>"; /* Option values are added by looping through the array */ } echo "</select>";// Closing of list box ?> |
Paul Reay | 18-07-2014 |
I'm finding that unless the option is clicked on then its value isn't being stored, so what if i actually want to use the selected value and just tab to next field on my form, at the moment it wont store the value. |
smo | 25-09-2014 |
For this you need Ajax. By this when you use tab and move away to next field the value will automatically stored without submitting the form. You can also give a message asking user to select first before moving out or submit the form. |
mitch | 25-11-2014 |
thanks so so much |
anusha | 18-12-2014 |
plz send me the 4 dropdown list filter search in php using mysql |
ross | 18-01-2015 |
I get a drop box that is empty with no label What am I doing wrong??? $sql="SELECT supplier FROM supplier_name order by supplier"; /* You can add order by clause to the sql statement if the names are to be displayed in alphabetical order */ echo "<select name=supplier value=Supplier>"; // list box select command foreach ($dbo->query($sql) as $row){//Array or records stored in $row echo "<option value=$row[id]>$row[name]</option>"; /* Option values are added by looping through the array */ } echo "</select>";// Closing of list box |
smo | 18-01-2015 |
In your query you are asking only one column supplier, then while displaying you are asking $row[id] and $row[name]. You must select all the columns you will be using inside dropdown list box. Or you can call all the columns by using wildcard * . $sql="SELECT * FROM supplier_name order by supplier"; |
sheriff | 10-02-2015 |
i need help!!! i have two items in drop down of my html page.so i need that if i selected first item in my dropdown,it should fetch some xyz data from the database and if i select second item from drop down,it should show other details from the same data base. what i have to do for that.... |
smo | 10-02-2015 |
You can read double dropdown listbox to get the matching records from database. The |
sara | 21-05-2015 |
i want to fetch data from data base by choosing item from two dropdown list............... |
Adam | 03-12-2015 |
Question How would create a drop down menu with the id numbers instead of names Thanks |
smo1234 | 04-12-2015 |
Check the line saying echo "<option value=$row[id]>$row[name]</option>"; Change this to echo "<option value=$row[name]>$row[id]</option>"; |
MOnish | 11-12-2015 |
how can i change the script to my needs ?? |
kishan | 12-12-2015 |
Sir i can not retrive an image from mysql in dropdownlist |
dhananjay | 06-06-2016 |
search record by dropdown list from data base is should show in table format my table name is gn_user_record. columns in table are id, username, password, email, pan_number. it should show by pan number. code in php mysql |
hackerz12on | 24-11-2017 |
After the drop down is sucessfully created, if the user clicks a value from drop down for example. IF he clicks John Deo then Four needs to be displayed in <div> . How can i make this happen ?? |
smo1234 | 25-11-2017 |
You can use onselect event of the dropdown list to display the div with matching content. You can use JavaScript or JQuery. Here you can see dropdown list with JQuery. |
xyz | 26-01-2019 |
I want a code that will select only the selected value from drop down list and then match the value with the database available in php |
Helly | 29-05-2019 |
if i have a drop down of country,state,city and area after clicking on submit button it will show the societyname or apartment name , utility, charge and all data on table how can we do that |