如何使用AJAX从数据库中获取价值?当我选择部门自动填充员工姓名然后选择员工姓名自动填写银行A / C号和基本工资时,我正在创建工资单。我尝试使用此代码,但在最后一次下拉中遇到问题。
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编码
<?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
}
}
?>
我的数据库文件:
<?php
mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("payslip") or die(mysql_error());
?>
答案 0 :(得分:0)
这不是解决方案,只是尝试调试代码。
请阅读PHP。
error_reporting(E_ERROR | E_WARNING | E_PARSE);
$conn = mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("payslip") or die(mysql_error());
if (isset($_GET['m'])) {
$ko = $_GET['m'];
$sel5 = mysql_query("select basicsalary from salary where ac='$ko'", $conn);
if (!$sel5) {
die(mysql_error());
}
$options = "";
while ($i1 = mysql_fetch_array($sel5, MYSQL_NUM)) {
$options .= "<option value={$i1[0]}>{$i1[0]}</option>\r\n";
}
mysql_close($conn);
echo $options;
} else {
echo "empty data request";
}