数据库信息:
CREATE TABLE
IF NOT EXISTS tz_customer (
id INT (10) NOT NULL,
customervarchar (255) NOT NULL,
NAME VARCHAR (255) NOT NULL,
vs_1 VARCHAR (255) NOT NULL,
stamp VARCHAR (255) NOT NULL
)
和PHP代码
这是index.php文件
<select name="nimetus" id="nimetus" onchange="chg(this.value)">
<?php $q3 = mysql_query("SELECT * FROM tz_kliendid"); while ($f3 = mysql_fetch_array($q3)) { ?>
<option value="<?php echo $f3['id']; ?>"><?php echo $f3['klient']; ?></option><?php } ?></select>
<input type="text" id="target" value="">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script type="text/javascript">function chg(str){
$.ajax({
url: "showdata.php",
type: "POST",
data: {
"val": str
},
success: function (response) {
$("#target").val(response);
},
error: function (xhr) {
alert("Some error found!!");
}
});
}</script>
AND showdata.php文件
<?php if(isset($_GET['val'])){$nimetus=$_POST['nimetus'];
$q1=mysql_query("SELECT vs_1 FROM tz_kliendid WHERE id='".$nimetus."' ORDER BY id DESC LIMIT 1"); $f1=mysql_fetch_array($q1); echo $f1['vs_1']; } ?>
每个客户都有一个标记为0或1的“tz customer”表“vs_1”列。 当我选择客户时,如何从表中获得所选客户的“vs_1”值?
和
如何获取所选客户的值以回显<input>
元素?
答案 0 :(得分:0)
您好我完全不了解您的问题,因为我可以理解您在更改选择时需要vs_1每个客户的价值。您需要将您的选项值更改为ID,因为它是主要的。您需要ajax到这样做
<select name="nimetus" id="nimetus" onchange="chg(this.value)">
<?php
while ($f3 = mysql_fetch_array($q3)) {
?>
<option value="<?php echo $f3['id']; ?>">
<?php echo $f3['customer']; ?>
</option>
<?php
}
?>
</select>
<input type="text" id="target" value="">
在java脚本中
function chg(str){
$.ajax({
url: "showdata.php",
type: "POST", //send it through post method
data: {
"val": str
},
success: function (response) {
//Do Something
$("#target").val(response);
},
error: function (xhr) {
//Do Something to handle error
alert("Some error found!!");
}
});
}
在showdata.php中
if(isset($_POST['val']))//your POST method
{
$nimetus=$_POST['val'];
$q1=mysql_query("SELECT vs_1 FROM tz_kliendid WHERE id='".$nimetus."' ORDER BY id DESC LIMIT 1");
$f1=mysql_fetch_array($q1);
echo $f1['vs_1'];
}
答案 1 :(得分:0)
您可以使用data-attribute来保存v1
的值,然后使用jquery访问它以显示它,通过这样做,每次更改值时都不需要额外的ajax调用
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="UTF-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
</head>
<body>
<?php
$option = '';
$q3 = mysql_query("SELECT * FROM tz_customer");
while ($f3 = mysql_fetch_array($q3)) {
$option .= '
<option value="' . $f3['customer'] . '"' . ($f3['id'] == $f3['customer'] ? " selected" : "") . ' data-v1="'.$f3['v1'].'">
' . $f3['customer'] .
'</option>';
} ?>
<select name="nimetus" id="nimetus">
<?= $option ?>
</select>
<div id="output"></div>
<script>
$(function() {
$('#cbo').on('change', function() {
var opt = $('option:selected', $(this));
$('#output').html(opt.text()+ ' => v1 :'+ opt.data('v1'));
});
});
</script>
</body>
</html>