我尝试将index.php发送发布请求文件发送到文件request.php并将request.php数据发送到表单并将数据json_decode打印到文件。
索引.php
<!DOCTYPE html>
<html>
<head>
<title>send request</title>
<!--- jQuery cdn ----->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<div class="display-data"></div>
</body>
</html>
<script type='text/javascript'>
$(document).ready(function () {
$.post('request.php', function (data, status) {
if (status == 'success') {
$('.display-data').append(data);
}
})
});
</script>
请求.php
<?php
//This is a create a connection
$servername = 'localhost';
$dbname = "stackoverflow";
$username = "root";
$pass = "";
$conn = new PDO("mysql:host=$servername; dbname=$dbname;", "$username", "$pass");
//This is a create a connection end
$selectSql = "SELECT * FROM tblstudent";
$select = $conn->prepare($selectSql);
$select->execute();
for($i=0; $i<3; $i++) {
$data = $select->fetch();
$img[] = $data['image']; //This is a array of image
$name[] = $data['name']; //This is a array of name
$score[] = $data['score']; //This is a array of score
}
for($i=0; $i<3; $i++) {
echo $img[$i]."<br>"; //Print data to index.php file
echo $name[$i].'<br>'; //Print data to index.php file
echo $score[$i]."<br>"; //Print data to index.php file
echo '<br><br>';
}
?>