我對整個javascript和PHP都很陌生。我的代碼有兩個問題似乎無法修復(fù)。
當我選擇一個類別時,它應(yīng)該顯示一個具有相應(yīng)類別的表。但是它包含了一個重復(fù)的下拉列表,還有一個錯誤,我不明白為什么返回為“未定義的變量:在/var/www/html/phpDD.php第37行的輸出”,它是:“$output.=”tbody“。
希望你能幫助我繼續(xù)發(fā)展!:)
phpDD.php
<?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";
}
?>
Index.php
<!Doctype html>
<html lang="en">
<head>
>
<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>
問題是您沒有在腳本中定義
$output
。不能讓$output
的第一個實例是$output .=
,因為.=
表示變量已經(jīng)存在。要解決這個問題,請在
$output .=
之前添加$output = '';
For example:
至于副本,在使用AJAX加載內(nèi)容時,包含了主文件
testSide.php
。