下面是ajax部分。这样就返回了一行:
appleorangebanana
我想要达到的目标是:
苹果
橙
香蕉
作为可点击链接。抱歉,我还是ajax& JSON。感谢您查询我的询问。
<div class="col-md-4" id="testing">
</div>
<script>
$(document).ready(function(){
$(".nav > a").click(function(e){ // see change here
e.preventDefault();
$.post("test.php",
{
value:$(this).text().trim()
},
function(data)
{
$("#testing").html(data.replace(/\"/g,""));//to remove quotes from the output
}
);
});
});
data.php
<?php require('../config/connection.php');
if(isset($_POST['value'])){
$value = $_POST['value'];
$query = "SELECT DISTINCT product FROM prdct_categories WHERE class = '$value'";
$result = mysqli_query($dbc, $query);
while($row = mysqli_fetch_assoc($result)){
$rowvalue = $row['product'];
echo json_encode($rowvalue);
}
}
?>
答案 0 :(得分:0)
可能(或可能不会,取决于您的应用程序)更容易在后端构建整个HTML并直接将其提供给您的JS AJAX调用。假设您愿意采用这条路径,您可以在后端脚本中执行以下操作:
// You MUST take care of a possible SQL injection!
// There can be ANYTHING in the $value.
$query = "SELECT DISTINCT product FROM prdct_categories WHERE class = '". mysql_real_escape_string($value) ."'";
$result = mysqli_query($dbc, $query);
// Let's build a nice unordered list out of this.
$html = '<ul>';
while($row = mysqli_fetch_assoc($result)){
$clickable_url = ''; // something meaningful must be here, of course.
$html .= '<li>';
$html .= '<a href="'. $clickable_url .'">'. $row['product'] .'</a>';
$html .= '</li>';
}
$html .= '</ul>';
echo $html;
在你的AJAX前端,它只是将这个新HTML添加到你的DOM中:
$.post("test.php",
{
value:$(this).text().trim()
},
function(data)
{
$("#testing").html(data);
}
);