我是新的AJAX和JQuery。我试图用它来调用两个PHP脚本。我在网上找到了一些例子,但只是为了调用函数。我只是试图调用这些脚本,以便它将加载我的主PHP文件中的所有内容,然后将显示在屏幕上,而不会刷新页面。如何使用AJAX/JQuery调用多个PHP脚本?
这里是小提琴例如,它的工作原理,如果我把我所有的PHP脚本在一个文件中:http://jsfiddle.net/vw4w3ay5/
在预先感谢你的帮助是非常赞赏!
main_php文件(其中我想打电话给我其他的PHP脚本):
<div id="map_size" align="center">
<script type="text/javascript">
/*I WANT TO CALL THE TWO SCRIPTS BEFORE EXECUTE THE FUNCTION BELOW*/
$(".desk_box").click(function() {
$(".station_info").hide(); // to hide all the others.
$("#station_info"+ $(this).attr('data')).show();
});
</script>
display_desk.php(剧本我想打电话):
<?php
include 'db_conn.php';
//query to get X,Y coordinates from DB for the DESKS
$desk_coord_sql = "SELECT coordinate_id, x_coord, y_coord FROM coordinates";
$desk_coord_result = mysqli_query($conn,$desk_coord_sql);
//see if query is good
if($desk_coord_result === false) {
die(mysqli_error());
}
//didsplay Desk stations in the map
while($row = mysqli_fetch_assoc($desk_coord_result)){
//naming X,Y values
$id = $row['coordinate_id'];
$x_pos = $row['x_coord'];
$y_pos = $row['y_coord'];
//draw a box with a DIV at its X,Y coord
echo "<div class='desk_box' data='".$id."' style='position:absolute;left:".$x_pos."px;top:".$y_pos."px;'>id:".$id."</div>";
} //end while loop for desk_coord_result
mysqli_close($conn); // <-- DO I NEED TO INCLUDE IT HERE OR IN MY db_conn.php SINCE IM INCLUDING IT AT THE TOP?
?>
display_stationinfo.php(第二脚本我想打电话):
<?php
include 'db_conn.php';
//query to show workstation/desks information from DB for the DESKS
$station_sql = "SELECT coordinate_id, x_coord, y_coord, section_name FROM coordinates";
$station_result = mysqli_query($conn,$station_sql);
//see if query is good
if($station_result === false) {
die(mysqli_error());
}
//Display workstations information in a hidden DIV that is toggled
while($row = mysqli_fetch_assoc($station_result)){
//naming values
$id = $row['coordinate_id'];
$x_pos = $row['x_coord'];
$y_pos = $row['y_coord'];
$sec_name = $row['section_name'];
//display DIV with the content inside
echo "<div class='station_info' id='station_info".$id."' style='position:absolute;left:".$x_pos."px;top:".$y_pos."px;'>Hello the id is:".$id."</br>Section:".$sec_name."</br></div>";
}//end while loop for station_result
mysqli_close($conn); // <-- DO I NEED TO INCLUDE IT HERE OR IN MY db_conn.php SINCE IM INCLUDING IT AT THE TOP?
>
以及如果使用嵌套ajax进行操作? – 2014-10-08 15:47:08
我想这样做:$(文件)。就绪(函数(){ \t \t \t \t \t $阿贾克斯({ \t \t \t \t \t \t \t \t帖子: “GET”, \t \t \t \t \t \t \t \t url:“display_desk.php” \t \t \t \t \t \t \t \t})。DONE(功能(数据){ \t \t \t \t \t \t \t \t \t \t警报(数据); \t \t \t \t \t \t \t \t \t \t \t \t})。失败(函数(jqXHR,textStatus,errorThrown){ \t \t \t \t \t \t \t \t \t \t \t \t alert(textStatus); \t \t \t \t \t \t \t \t \t \t \t \t \t});但不工作 – mario 2014-10-08 16:52:09