簡體   English   中英

如何使用AJAX / JQuery調用多個PHP腳本?

[英]How can I call multiple PHP scripts with AJAX/JQuery?

我是AJAX和JQuery的新手。 我試圖用它來調用兩個PHP腳本。 我在網上找到了一些示例,但僅用於調用函數。 我只是試圖調用腳本,以便它將所有內容加載到我的主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?

?>

關於什么?

<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>

確保添加$(document).ready(function(){

});

/ 編輯 /嗡嗡聲,您想使用Ajax。 您嘗試過:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM