0 Replies - 434 Views - Last Post: 12 August 2009 - 02:09 AM

#1 noorahmad  Icon User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

PHP and AJAX: get data from database using combobox

Posted 12 August 2009 - 02:09 AM

Description: this code will help you how to get data from database with combo box using PHP and AJAX (jQuery)
//<!---------- index.php ------------------>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
	$(document).ready(function(){
		$("#names").change(function(){
			$.post('php.php',{id:$(this).val(),rnd:Math.random()},function(data){
				$("#div_show").html(data);
			});
		});
	});
</script>
<?php 
include('conn.php');
$query = mysql_query("SELECT * FROM tbl_emp")or die(mysql_error());
?>
<style type="text/css">
<!--
.style1 {
	font-size: 10px;
	font-family: Verdana, Arial, Helvetica, sans-serif;
}
-->
</style>

<table width="300" border="0" cellspacing="0" cellpadding="0">
    <tr>
      <td><span class="style1">Select Name:</span></td>
      <td>
        <select name="names" id="names">
<?PHP
while($fetch = mysql_fetch_assoc($query)){
	echo "<option value=".$fetch['id'].">".$fetch['first_name']." ".$fetch['last_name']."</optgroup>";
}
?>
        </select>
	  </td>
    </tr>
</table>

  <div id="div_show"></div>
  <p> </p>
<?php 
$query_emp = mysql_query('SELECT * FROM tbl_emp');
if($query_emp){
	if(mysql_num_rows($query_emp)>0){
?>
<style type="text/css">
<!--
.style3 {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; }
-->
</style>
<table width="600" border="0" cellspacing="1" cellpadding="2">
  <tr>
    <td width="10%" bgcolor="#999999"><span class="style3">ID</span></td>
    <td width="10%" bgcolor="#999999"><span class="style3">First Name</span></td>
    <td width="10%" bgcolor="#999999"><span class="style3">Last Name</span></td>
    <td width="10%" bgcolor="#999999"><span class="style3">Age</span></td>
    <td width="10%" bgcolor="#999999"><span class="style3">Place</span></td>
    <td width="10%" bgcolor="#999999"><span class="style3">Job</span></td>
  </tr>
<?php
while($fetch_emp = mysql_fetch_assoc($query_emp)){
?>
  <tr>
    <td width="10%" bgcolor="#CCCCCC"><span class="style3"><?php echo $fetch_emp['id'];?></span></td>
    <td width="10%" bgcolor="#CCCCCC"><span class="style3"><?php echo $fetch_emp['first_name'];?></span></td>
    <td width="10%" bgcolor="#CCCCCC"><span class="style3"><?php echo $fetch_emp['last_name'];?></span></td>
    <td width="10%" bgcolor="#CCCCCC"><span class="style3"><?php echo $fetch_emp['age'];?></span></td>
    <td width="10%" bgcolor="#CCCCCC"><span class="style3"><?php echo $fetch_emp['place'];?></span></td>
    <td width="10%" bgcolor="#CCCCCC"><span class="style3"><?php echo $fetch_emp['job'];?></span></td>
  </tr>
<?php
}
}}
?>
</table>


//<!------------------ php.php page ------------------->
<?php
include('conn.php');
if(isset($_POST['id'])){
	$query_emp = mysql_query('SELECT * FROM tbl_emp where id='.$_POST['id']);
	if($query_emp){
		if(mysql_num_rows($query_emp)>0){
?>
<style type="text/css">
<!--
.style3 {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; }
-->
</style>

<p> </p>
<table width="600" border="0" cellspacing="1" cellpadding="2">
  <tr>
    <td width="10%" bgcolor="#999999"><span class="style3">ID</span></td>
    <td width="10%" bgcolor="#999999"><span class="style3">First Name</span></td>
    <td width="10%" bgcolor="#999999"><span class="style3">Last Name</span></td>
    <td width="10%" bgcolor="#999999"><span class="style3">Age</span></td>
    <td width="10%" bgcolor="#999999"><span class="style3">Place</span></td>
    <td width="10%" bgcolor="#999999"><span class="style3">Job</span></td>
  </tr>
<?php
while($fetch_emp = mysql_fetch_assoc($query_emp)){
?>
  <tr>
    <td width="10%" bgcolor="#CCCCCC"><span class="style3"><?php echo $fetch_emp['id'];?></span></td>
    <td width="10%" bgcolor="#CCCCCC"><span class="style3"><?php echo $fetch_emp['first_name'];?></span></td>
    <td width="10%" bgcolor="#CCCCCC"><span class="style3"><?php echo $fetch_emp['last_name'];?></span></td>
    <td width="10%" bgcolor="#CCCCCC"><span class="style3"><?php echo $fetch_emp['age'];?></span></td>
    <td width="10%" bgcolor="#CCCCCC"><span class="style3"><?php echo $fetch_emp['place'];?></span></td>
    <td width="10%" bgcolor="#CCCCCC"><span class="style3"><?php echo $fetch_emp['job'];?></span></td>
  </tr>
<?php
}
?>
</table>
<?php 		
		}else{
			echo "Sorry no record found";
		}
	}else{
		echo "Sorry Query Faild";
	}
}
?>


//<!---------- conn.php -------------->

<?php
$con = mysql_connect('localhost','root','')or die(mysql_error());
$db = "CREATE DATABASE IF NOT EXISTS `ajax` ;";
$db_query = mysql_query($db)or die(mysql_error());
$db_select = mysql_select_db('ajax')or die(mysql_error());
$tbl = "CREATE TABLE IF NOT EXISTS `tbl_emp` (
		`id` int(11) NOT NULL AUTO_INCREMENT,
		`first_name` varchar(100) NOT NULL,
		`last_name` varchar(100) NOT NULL,
		`age` int(11) NOT NULL,
		`place` varchar(100) NOT NULL,
		`job` varchar(100) NOT NULL,
		PRIMARY KEY (`id`)
		) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;";
$tbl_query = mysql_query($tbl)or die(mysql_error());
$tbl_data =
		"INSERT INTO `tbl_emp` (`first_name`, `last_name`, `age`, `place`, `job`) VALUES
		('peter', 'yianni', 25, 'US', 'PHP Developer'),
		('Noor', 'Ahmad', 21, '?', 'Web Developer'),
		('janee', 'panee', 16, 'IN', 'Web Designer'),
		('janee', 'panee', 16, 'IN', 'Web Designer');
		";
$tbl_data = mysql_query($tbl_data)or die(mysql_error());
?>


Is This A Good Question/Topic? 0
  • +

Page 1 of 1