我想练习使用AJAX。我有一个MySQL数据库,并且我在列storeName中插入了一些名字。我做了一个SQL查询,输出数据库中列的随机文本。这是工作的罚款,但只有当我刷新我的浏览器:随机数与AJAX
<?php
$sql ="SELECT * FROM stores ORDER BY RAND () LIMIT 1";
$res = $mysqli->query($sql);
//print($res);
if ($res->num_rows > 0) {
// output data of each row
while($row = $res->fetch_assoc()) {
echo "Storename: " . $row["storeName"]. "<br>" .
}
} else {
echo "0 results";
}
?>
我想提出一个AJAX功能,无需我要刷新我的浏览器调用我的SQL查询。我试着做下面的代码,但是当我点击shuffle按钮时,什么都没有发生。我在这里做错了什么?
<body>
<div id="shuffle" >
<h4>Shuffle Stores</h4>
<!-- This php function loads everytime I refresh the browser -->
<?php include 'function/select_shuffle.php' ?>
<form action="function/select_shuffle.php" method="post">
<button>shuffle</button>
</form>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<!-- Shuffle AJAX method-->
<script>
$(document).ready(function(){
$("button").click(function(){
$.ajax({url: "function/select_shuffle.php", success: function(result){
$("#shuffle").html(result);
}});
});
});
</script>
</body>
是否调用成功回调? – Carcigenicate
感谢您的评论。不,那不叫。其实我现在可以看到,当我点击“Shuffle”按钮时,出现localhost当前无法处理此请求的错误。 HTTP ERROR 500' – Mimi
您无法阻止表单通过默认流程进行提交。 – charlietfl