2014-10-08 78 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