Javascript 选择下拉列表值时如何删除下拉列表的重复项
我对整个javascript和PHP都很陌生。我的代码有两个问题似乎无法解决 当我选择一个类别时,它应该显示一个带有相应类别的表。但它包含了作为副本的下拉列表,还有一个错误,我不明白为什么会作为“未定义变量:第37行/var/www/html/phpDD.php中的输出”返回,即:“$output.=”tbody” 我希望你能帮助我继续发展!:) phpDD.phpJavascript 选择下拉列表值时如何删除下拉列表的重复项,javascript,php,html,Javascript,Php,Html,我对整个javascript和PHP都很陌生。我的代码有两个问题似乎无法解决 当我选择一个类别时,它应该显示一个带有相应类别的表。但它包含了作为副本的下拉列表,还有一个错误,我不明白为什么会作为“未定义变量:第37行/var/www/html/phpDD.php中的输出”返回,即:“$output.=”tbody” 我希望你能帮助我继续发展!:) phpDD.php <?php // include database connection file include_onc
<?php
// include database connection file
include_once "testSide.php";
if(isset($_POST['InvestmentName']))
{
$uid = $_POST['InvestmentName'];
}
$qu = "select Prod.name, one.yield as yield_one, five.yield as yield_two, ten.yield as yield_three, twenty.yield as yield_four, one.stdev as st_one, five.stdev as st_two, ten.stdev as st_three, twenty.stdev as st_four, one.top1dd as top_one, five.top1dd as top_two, ten.top1dd as top_three, twenty.top1dd as top_four Select *
from products
where category_id = $uid) as Prod
left join;";
$result = $conn->query($qu);
if (!$result) {
trigger_error('Invalid query:' . $conn->error);
}
if ($result->rowCount() > 0) {
$output .= "<table class='table table-hover table-border'>
<thead>
<tr>
<th>Name</th>
<th>Afkast sidste år</th>
<th>Genst sidste 5 år</th>
<th>Genst stidste 10 år</th>
<th>Genst sidste 20 år</th>
<th>SR sidste år</th>
<th>SR 5 år</th>
<th>SR 10 år</th>
<th>SR 20 år</th>
<th>DD sidste år</th>
<th>DD 5 år</th>
<th>DD 10 år</th>
<th>DD 20 år</th>
</tr>
</thead>";
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
$output .= "<tbody>
<tr>
<td>{$row["name"]}</td>
<td>{$row["yield_one"]}</td>
<td>{$row["yield_two"]}</td>
<td>{$row["yield_three"]}</td>
<td>{$row["yield_four"]}</td>
<td>{$row["st_one"]}</td>
<td>{$row["st_two"]}</td>
<td>{$row["st_three"]}</td>
<td>{$row["st_four"]}</td>
<td>{$row["top_one"]}</td>
<td>{$row["top_two"]}</td>
<td>{$row["top_three"]}</td>
<td>{$row["top_four"]}</td>
</tr>";
}
"</tbody>";
$output .= "</tbody></table>";
echo $output;
}else{
echo "No records found";
}
?>
<!Doctype html>
<html lang="en">
<head>
href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container" style="margin-top: 50px;">
<h2 class="text-center">Væg en kategori </h2>
<div class="row">
<div class="col-md-4"></div>
<div class="col-md-4" style="margin-top:20px; margin-bottom:20px;">
<form id="submitForm">
<div class="form-group">
<select class="form-control Investment" name="Investment" id="Investment">
<option value="">Vælg Kategori</option>
<?php foreach($users as $user): ?>
<option value="<?= $user['id']; ?>"><?= $user['name']; ?></option>
<?php endforeach; ?>
</select>
</div>
</form>
</div>
</div>
<div class="col-md-12">
<div id="show-invest">
</div>
</div>
</div>
</body>
</html>
<!---jQuery ajax load rcords using select box --->
<script type="text/javascript">
$(document).ready(function(){
$(".Investment").on("change", function(){
var InvestmentName = $(this).val();
if (InvestmentName !== "") {
$.ajax({
url : "phpDD.php",
type:"POST",
cache:false,
data:{InvestmentName:InvestmentName},
success:function(data){
$("#show-invest").html(data);
}
});
}else{
$("#show-invest").html(" ");
}
})
});
</script>
$(文档).ready(函数(){
美元(“.Investment”)。关于(“更改”,函数(){
var InvestmentName=$(this.val();
如果(投资名称!==“”){
$.ajax({
url:“phpDD.php”,
类型:“POST”,
cache:false,
数据:{InvestmentName:InvestmentName},
成功:功能(数据){
$(“#show invest”).html(数据);
}
});
}否则{
$(“#show invest”).html(“”);
}
})
});
网站
DB
问题在于您没有在脚本中定义
$output
。您不能将$output
的第一个实例设置为$output.=
,因为=
表示变量已经存在
要解决此问题,请在$output=''之前的某个位置添加$output='';
。=
例如:
<?php
// include database connection file
include_once "testSide.php";
if(isset($_POST['InvestmentName']))
{
$uid = $_POST['InvestmentName'];
}
$output = ''; //This is required
$qu = "select Prod.name, one.yield as yield_one, five.yield as yield_two, ten.yield as yield_three, twenty.yield as yield_four, one.stdev as st_one, five.stdev as st_two, ten.stdev as st_three, twenty.stdev as st_four, one.top1dd as top_one, five.top1dd as top_two, ten.top1dd as top_three, twenty.top1dd as top_four Select *
from products
where category_id = $uid) as Prod
left join;";
$result = $conn->query($qu);
if (!$result) {
trigger_error('Invalid query:' . $conn->error);
}
if ($result->rowCount() > 0) {
$output .= "<table class='table table-hover table-border'>
<thead>
<tr>
<th>Name</th>
<th>Afkast sidste år</th>
<th>Genst sidste 5 år</th>
<th>Genst stidste 10 år</th>
<th>Genst sidste 20 år</th>
<th>SR sidste år</th>
<th>SR 5 år</th>
<th>SR 10 år</th>
<th>SR 20 år</th>
<th>DD sidste år</th>
<th>DD 5 år</th>
<th>DD 10 år</th>
<th>DD 20 år</th>
</tr>
</thead>";
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
$output .= "<tbody>
<tr>
<td>{$row["name"]}</td>
<td>{$row["yield_one"]}</td>
<td>{$row["yield_two"]}</td>
<td>{$row["yield_three"]}</td>
<td>{$row["yield_four"]}</td>
<td>{$row["st_one"]}</td>
<td>{$row["st_two"]}</td>
<td>{$row["st_three"]}</td>
<td>{$row["st_four"]}</td>
<td>{$row["top_one"]}</td>
<td>{$row["top_two"]}</td>
<td>{$row["top_three"]}</td>
<td>{$row["top_four"]}</td>
</tr>";
}
"</tbody>";
$output .= "</tbody></table>";
echo $output;
}else{
echo "No records found";
}
?>
Index
“未定义变量:第37行/var/www/html/phpDD.php中的输出”
如果查找$output
的第一个实例,它是:
$output .= "<table class='table table-hover table-border'>
但是,如果$output
在if
块之外是echo()
'd,那么我们必须在if
块之外/之前声明它:
$output = '';
if ($result->rowCount() > 0) {
$output .= "<table class='table table-hover table-border'>
...
}
echo $output;
$output='';
如果($result->rowCount()>0){
$output.=”
...
}
echo$输出;
您可以显示您的数据库吗?这是完整的phpDD.php吗?(它没有"你能展示完整的phpDD吗?PHP警告:你对参数化预处理语句持开放态度,应该使用参数化预处理语句,而不是手动生成查询。它们是由或提供的。永远不要相信任何类型的输入!即使你的查询仅由受信任的用户执行。你能复制准确的错误消息吗?谢谢!它现在起作用了!你能看到w吗为什么它也会出现两个下拉列表?你能添加副本的屏幕截图吗?它应该在数据库上方的图片中:)你可以看到“vælg en katego”,下拉列表有两次我添加了一个标签“网站”:
$output = '';
if ($result->rowCount() > 0) {
$output .= "<table class='table table-hover table-border'>
...
}
echo $output;