Home LearnHow to query a database using ajax php drop down

How to query a database using ajax php drop down

by Buhle Dlamini
How to query a database using ajax php drop down
Spread the love

How to query a database using ajax php drop down

Save this as main.php

<html>
  <script src="//code.jquery.com/jquery-1.9.1.js"></script>
    <script src="//ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js"></script>


<!--onchange function-->
<script type="text/javascript">
function empidChange(val)
{
//alert(1);
url="content.php";
data="empid="+val;
$.post(url,data,function(data){
$("#responsecontainer").html(data);
});

}
</script>
<body>
<select id="empid" name="empid" onChange="empidChange(this.value)" >
<option value="0">Sselect id</option>
<option value="1">Musa</option>
<option value="2">Birmingham</option>
</select>

<div id="responsecontainer" align="center">

</div>
</body>
</html>

Save this as content.php

<?php
// connect  to the database 
require_once('dbconnect.php'); 
?>
<?php
$empid = $_POST['empid'];

$get_member =" SELECT 
empid, lastName, firstName, email, usercode, companyid, userid, jobTitle, cell, employeetype, address ,initials   FROM employees
WHERE empid ='$empid'
";
$user_coder1 = $con->prepare($get_member);
$user_coder1 ->execute();


while($row =$user_coder1->fetch(PDO::FETCH_ASSOC)){
$firstName = $row['firstName'];
$empid = $row['empid'];
$lastName =    $row['lastName'];
$cell =    $row['cell'];

   echo $firstName;
   echo $empid;
   echo $lastName;

}

?>

Related Articles

Leave a Comment