Populate a Dropdown and Display a Related Record in PHP


Here in the dropdown we have one column record, which is populated, and also there is a list named as ALL in the dropdown. When we select ALL, then all the records from the database will be displayed. The connection between PHP scripts and the MySQL database is done in the config.php file.

Table creation

Server: localhost   Database: home_work
-- phpMyAdmin SQL Dump
-- version 2.10.1
-- http://www.phpmyadmin.net

--
-- Host: localhost
-- Generation Time: Nov 17, 2011 at 05:17 PM
-- Server version: 5.0.45
-- PHP Version: 5.2.5
 
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
 
--
-- Database: `home_work`
--
 
-- -------------------------------------------------------- 
--
-- Table structure for table `dropdown`
--
 
CREATE TABLE `dropdown` (
  `id` int(10) NOT NULL auto_increment,
  `sid` varchar(255) NOT NULL,
  `sname` varchar(255) NOT NULL,
  `age` int(11) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ; 
--
-- Dumping data for table `dropdown`
--
INSERT INTO `dropdown` (`id`, `sid`, `sname`, `age`) VALUES
(1, 's001', 'raj', 25),
(2, 's002', 'rahul', 26),
(3, 's003', 'raju', 27),
(4, 's004', 'rajesh', 28);
 
config.php
 
<?php
$DBHOST = "localhost";
$DBNAME = "home_work";
$DBUSER = "root";
 
$sLink = mysql_connect($DBHOST,$DBUSER,'') or die('Connection with MySql Server failed');
mysql_select_db($DBNAME, $sLink) or die('MySql DB was not found');
?> 

showDetails1.php
 
<?php
$link = mysql_connect("localhost","root","");
mysql_select_db("home_work",$link);
$sql = "SELECT * FROM dropdown ";
$aResult = mysql_query($sql);
if($_REQUEST['frm_action'] == 3)
{
if ($_REQUEST['cust_id'] == 0)
{
$id = $_REQUEST['cust_id'];
$sqlCustomer = "SELECT * FROM dropdown ";
}
else
{
$id = $_REQUEST['cust_id'];
$sqlCustomer = "SELECT * FROM dropdown WHERE id ='$id'";
}
$aCustomer = mysql_query($sqlCustomer);
}
?>
<html>
<head>
<script type="text/javascript">
function changeSID()
{
oForm       = eval(document.getElementById("frmForm"));
iCustomerId = document.getElementById("sid").value;
url         = "showDetails1.php?frm_action=3&cust_id=" +iCustomerId;
document.location = url;
}
</script>
</head>

<body>
<form name="frmForm" id="frmForm" >
<table border="0" cellspacing="2" cellpadding="2" width="40%">
<tr>
<td align="right" ><strong>Sid</strong></td>
<td align="left"><select name="sid" id="sid" onchange="javascript:changeSID();">
<option value="">Select</option>
<option value="0">All</option> 

<?php
$sid1 = $_REQUEST['cust_id'];

while($rows=mysql_fetch_array($aResult,MYSQL_ASSOC))
{
$id  = $rows['id'];
$sid = $rows['sid'];
if($sid1 == $id)
{
$chkselect = 'selected';

}
else
{
$chkselect ='';
}
?>
<option value="<?php echo  $id;?>"<?php echo $chkselect;?>><?php echo $sid;?></option>
<?php } ?>
</td>
</tr>
<?php if($_REQUEST['frm_action'] == 3) { ?>
<tr>
<td colspan="2">
<table  style="border:1px solid #003366;" cellspacing="2" cellpadding="2" width="100%" bgcolor="#003366">
<tr bgcolor="#EFEFEF">
<td><b><font color='Red'>Sid</font></b></td>
<td><b><font color='Red'>Sname</font></b></td>
<td><b><font color='Red'>Age</font></b></td>
</tr>
<?php
while($row1 = @mysql_fetch_array($aCustomer,MYSQL_ASSOC))
{
$sid   = $row1['sid'];
$sname = $row1['sname'];
$age   = $row1['age'];
?>
<tr bgcolor="#FFFFFF">
<td><b><font color='#663300'><?php echo $sid;?></font></b></td>
<td><b><font color='#663300'><?php echo $sname;?></font></b></td>
<td><b><font color='#663300'><?php echo $age;?></font></b></td>
</tr>
<?php } ?>
</table>
</td>
</tr>
<?php } ?>
</table>
</form>
</body>
</html>

Running the application

Run the WampServer then write the below line in the Url

http://localhost/Populate Dropdown box/

phpdropdown.gif

Up Next
    Ebook Download
    View all
    Learn
    View all