我有两个文件test.html和test.php。我想要做的是使用java脚本将数据从PHP文件显示到HTML。 test.php的:
<?php
$con = mysqli_connect('localhost','root','');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"autofetch");
$sql="SELECT * FROM sports ";
$result = mysqli_query($con,$sql);
$d =$_POST['iD'];
if($d==1)
{
echo '<div id="container"></div>';
echo '<div id="blocker" style="display: none;"></div>';
$response='';
while($row=mysqli_fetch_array($result)) {
$response = $response . " <li class='unread'>" .
"<h4>". $row["id"] . "</h4>" .
"<p>" . $row["sportname"] .
"</li>";
}
echo $response;
}
?>
的test.html:
<html>
<head>
<script>
$( window ).load(function() {
var url ='http://localhost/auto/test.php';
$.get(url,function(data){
$('#summary').html(data).show(1000);});
});
</script>
</head>
<body>
<div id="summary">
</div>
</body>
</html>
此外,我想在窗口加载时加载数据。我不知道自己哪里出错了。一切似乎都很好。
答案 0 :(得分:2)
删除$d =$_POST['iD'];
并且条件不是必需的。如果你想要的话。然后你必须在jquery中使用$ .post并将iD
作为数据
<?php
$con = mysqli_connect('localhost','root','');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"autofetch");
$sql="SELECT * FROM sports ";
$result = mysqli_query($con,$sql);
echo '<div id="container"></div>';
echo '<div id="blocker" style="display: none;"></div>';
$response='';
while($row=mysqli_fetch_array($result)) {
$response = $response . " <li class='unread'>" .
"<h4>". $row["id"] . "</h4>" .
"<p>" . $row["sportname"] .
"</li>";
}
echo $response;
?>