How can I get value from database using AJAX? I am creating salary slip in this when I select department auto populate employee name then select employee name auto populate Bank A/c No. and Basic salary. I tried using this code but getting problem in last drop down.
function dep(str) {
var a;
if (window.XMLHttpRequest) {
a = new XMLHttpRequest();
}
a.onreadystatechange = function () {
if (a.readyState == 4) {
document.getElementById("empid").innerHTML = a.responseText;
}
}
a.open("GET", "emp.php?j=" + str, true);
a.send();
}
function employee(str) {
//alert("hello");
var b;
if (window.XMLHttpRequest) {
b = new XMLHttpRequest();
}
b.onreadystatechange = function () {
if (b.readyState == 4) {
document.getElementById("kam").innerHTML = b.responseText;
}
}
b.open("GET", "ac.php?k=" + str, true);
b.send();
}
function ac(str) {
var d;
if (window.XMLHttpRequest) {
d = new XMLHttpRequest();
}
d.onreadystatechange = function () {
if (d.readyState == 4) {
document.getElementById("ja").innerHTML = d.responseText;
}
}
d.open("GET", "insert.php?m=" + str, true);
d.send();
}
PHP Coding
<?php
//error_reporting(0);
include("db.php");
if(isset($_REQUEST['j']))
{
$g=$_REQUEST['j'];
$sel=mysql_query("select * from employees where depid='$g'");
while($l=mysql_fetch_array($sel))
{
?>
<option value="<?php echo $l[0];?>"><?php echo $l[1]. " ". $l[2];?></option>
<?php
}
}
if(isset($_REQUEST['k']))
{
$o=$_REQUEST['k'];
echo $sel1=mysql_query("select ac from salary where empid='$o'");
while($i=mysql_fetch_array($sel1))
{
?>
<option value="<?php echo $i[0];?>"><?php echo $i[0];?></option>
<?php
}
}
if(isset($_REQUEST['m']))
{
$ko=$_REQUEST['m'];
echo $sel5=mysql_query("select basicsalary from salary where ac='$ko'");
while($i1=mysql_fetch_array($sel5))
{
?>
<option value="<?php echo $i1[0];?>"><?php echo $i1[0];?></option>
<?php
}
}
?>
my database file :
<?php
mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("payslip") or die(mysql_error());
?>