php中的JavaScript函数,在点击时显示更多信息(数据库查询)
JavaScript function within php to show more info on-click (Database Query)
下面的代码连接到保存客户信息的本地数据库。首先,页面应该显示客户名称和国家/地区,但一旦点击名称,应该显示该特定客户的更多信息。
下面这个代码的问题是,无论单击哪个名称,它都只会显示第一个客户信息。有人能看出我哪里出了问题吗?我需要为每个按钮(客户名称)创建一个ID吗?请记住,数据库中有很多客户,我觉得每个客户的ID都很难实现,尤其是在数据库被修改的情况下。
<DOCTYPE html!>
<html>
<head>
<link rel="stylesheet" type="text/css" href="a3.css">
</head>
<body>
<div class="nav">
<?php include 'nav.php';
?>
</div>
<?php include 'dbconfig.php';
$sql ='SELECT * FROM `customers` ORDER BY `customers`.`country` ASC';
$q = $conn->query($sql);
$q->setFetchMode(PDO::FETCH_ASSOC);
?>
<div class = "main">
<table>
<?php while ($r = $q->fetch()): ?>
<tr>
<td><button onclick = "showCustomer();"/><?php echo ($r['customerName'])?></td>
<td><?php echo ($r['country'])?></td>
</tr>
<tr id ="extra" style="display:none">
<td><?php echo ($r['customerNumber'])?></td>
<td><?php echo ($r['contactLastName'])?></td>
<td ><?php echo ($r['contactFirstName'])?></td>
<td><?php echo ($r['phone'])?></td>
<td><?php echo ($r['addressLine1'])?></td>
<td><?php echo ($r['addressLine2'])?></td>
<td><?php echo ($r['city'])?></td>
<td><?php echo ($r['state'])?></td>
<td><?php echo ($r['postalCode'])?></td>
<td><?php echo ($r['salesRepEmployeeNumber'])?> </td>
<td><?php echo ($r['creditLimit'])?></td>
</tr>
<?php endwhile; ?>
</table>
</div>
<div class="foot">
<?php include 'foot.php'; ?>
</div>
<script>
function showCustomer() {
var showForm=document.getElementById('extra');
if (showForm.style.display="none") {
showForm.style.display ="";
alert('ok');
console.log(showForm);
}
else if (showForm.style.display=""){
showForm.style.display ="none";
}
}
</script>
</body>
</html>
</DOCTYPE>
所有的<tr>
都有相同的id="extra"
。正如建议的那样,您需要为它们中的每一个都有一个不同的"id",这就是:我创建了一个变量$i
(第18行),它在while之后增加(第20行),作为参数插入到showCustomer
的调用中(第23行),插入到id"extra"的末尾(第27行),最后,在声明函数showCustomer
的底部,参数"index"插入到id的末尾。
<DOCTYPE html!>
<html>
<head>
<link rel="stylesheet" type="text/css" href="a3.css">
</head>
<body>
<div class="nav">
<?php include 'nav.php';
?>
</div>
<?php include 'dbconfig.php';
$sql ='SELECT * FROM `customers` ORDER BY `customers`.`country` ASC';
$q = $conn->query($sql);
$q->setFetchMode(PDO::FETCH_ASSOC);
?>
<div class = "main">
<table>
<?php $i = 0;
while ($r = $q->fetch()):
$i++; // UNIQUE INDEX FOR EACH CUSTOMER.
?>
<tr>
<td><button onclick = "showCustomer('<?php echo $i;?>');"/>
<?php echo ($r['customerName'])?></td>
<td><?php echo ($r['country'])?></td>
</tr>
<tr id ="extra<?php echo $i;?>" style="display:none">
<td><?php echo ($r['customerNumber'])?></td>
<td><?php echo ($r['contactLastName'])?></td>
<td ><?php echo ($r['contactFirstName'])?></td>
<td><?php echo ($r['phone'])?></td>
<td><?php echo ($r['addressLine1'])?></td>
<td><?php echo ($r['addressLine2'])?></td>
<td><?php echo ($r['city'])?></td>
<td><?php echo ($r['state'])?></td>
<td><?php echo ($r['postalCode'])?></td>
<td><?php echo ($r['salesRepEmployeeNumber'])?> </td>
<td><?php echo ($r['creditLimit'])?></td>
</tr>
<?php endwhile; ?>
</table>
</div>
<div class="foot">
<?php include 'foot.php'; ?>
</div>
<script>
function showCustomer( index ) { // UNIQUE INDEX AS PARAMETER.
var showForm=document.getElementById('extra' + index); // USING INDEX.
if (showForm.style.display="none") {
showForm.style.display ="";
alert('ok');
console.log(showForm);
}
else if (showForm.style.display=""){
showForm.style.display ="none";
}
}
</script>
</body>
</html>
</DOCTYPE>
相关文章:
- 包含数据库中相关信息的开放模态
- 谷歌地图-数据库中的标记只显示最后一行的信息
- 使用node-js上传文件,并将表单信息存储在数据库(mysql)中
- 如何从重定向的支付处理器网站提取交易详细信息并保存到我的rails 4数据库
- SuiteScript:如何有一个自定义的数据字段来从数据库加载信息
- 从下拉框中选择用户信息,并使用进度计数器显示数据库中的选定项目
- 使用 JS 和 PHP 从数据库中获取信息
- HTML5数据库保存了详细信息以及一个按钮
- php中的JavaScript函数,在点击时显示更多信息(数据库查询)
- 将信用卡信息临时存储在数据库中
- 如何从数据库中填充谷歌地图信息框
- 使用带有数据库信息的下拉菜单填充文本框
- 通过API将视频信息插入数据库
- Nodejs从数据库中检索信息并传递给JavaScript
- 从 SQL 数据库自动更新统计信息
- 单击时动态显示数据库信息
- 什么可能导致 MVC 应用在从数据库中提取后无法在 IE 中显示当前信息
- 如何在 html5 sql 数据库中存储表单信息
- 在 HTML 表格中显示从 Firebase 数据库中检索到的信息
- 阅读有关数据库结果的更多信息