I would like to practice on using AJAX. I have a MySQL database, and I have inserted some names in the column storeName. I made a SQL query that output random text in column in the database. That is working fine, but only when I refresh my browser:
<?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";
}
?>
I would like to make an AJAX function, that calls my SQL query without I have to refresh my browser. I tried to make the following code, but when I hit the shuffle button, nothing is happening. What am I doing wrong here?
<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>