2014-10-08 76 views
2

我是新的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? 

>

+0

以及如果使用嵌套ajax进行操作? – 2014-10-08 15:47:08

+0

我想这样做:$(文件)。就绪(函数(){ \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

回答

0

怎么样? :

<div id="map_size" align="center"> 
<?php 
echo "<script>"; 
include "display_desk.php"; 
include "display_stationinfo.php"; 
echo "</script>"; 
?> 
<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> 

要确保加$(文件)。就绪(函数(){

});

/编辑/ 哼,你想使用Ajax。您是否尝试过:

$.post("yourURL.php",function(html){ 
    /*here what you want to do*/ 
    /*return of your script in html*/ 
}); 
+0

在PHP中有什么不同,不只是把它放在我的JQuery函数的地方?我的display_stationinfo.php将会有数据,我将在稍后显示LIVE。我不只是想在我的主要php文件 – mario 2014-10-08 15:41:13

+0

“包含这些脚本”嗡嗡声你想使用Ajax。你尝试使用: $ .post(“yourURL.php”,function(html){ /*这里你想做什么*/ /*你的脚本返回在HTML */ }); – Buisson 2014-10-08 15:49:39

+0

你是什么意思“在这里你想做什么”?我只是想调用这些脚本,它会自行完成SQL查询并将其显示在主php文件中。 – mario 2014-10-08 16:41:03