ajax在页面加载

时间:2017-08-25 20:22:09

标签: jquery ajax

这就是情况。我有一个下拉菜单,当我更改其值时,查询将运行并显示一个表。我想要实现的是当页面加载时,即使没有点击下拉菜单,表格也会根据下拉菜单的默认选择值显示。

这是我的代码。 reservation.php

<script>

function showSched(str) {

        if (window.XMLHttpRequest) 
        {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else 
        {
            // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() 
        {
            if (this.readyState == 4 && this.status == 200) 
            {
                document.getElementById("schedDiv").innerHTML = this.responseText;
            }
        };
        xmlhttp.open("GET","getuser.php?q="+str,true);
        xmlhttp.send();
}
</script>

<div id="schedDiv">

</div>

getuser.php

<!DOCTYPE html>
<html>
<head>

<style>
table {
    width: 100%;
    border-collapse: collapse;
}

table, td, th {
    border: 1px solid black;
    padding: 5px;
}

th {text-align: left;}
</style>
</head>
<body>

<?php
$q = strval($_GET['q']);


    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "srdatabase";

    $con = new mysqli($servername, $username, $password, $dbname);

$sql="SELECT * FROM reservations WHERE speaker = '".$q."' AND reservationstatus = 'approved'";
$result = mysqli_query($con,$sql);

echo "<table class='reservations-table'>
<tr>
<th>Speaker</th>
<th>Reservation Date</th>
<th>Reservation Time</th>
<th>Location</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
    echo "<tr>";
    echo "<td>" . $row['speaker'] . "</td>";
    echo "<td>" . $row['date'] . "</td>";
    echo "<td>" . $row['time'] . "</td>";
    echo "<td>" . $row['location'] . "</td>";
    echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
</body>
</html>

0 个答案:

没有答案