简体   繁体   English

如何使用AJAX / JQuery调用多个PHP脚本?

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

I am new with AJAX and JQuery. 我是AJAX和JQuery的新手。 I am trying to use it to call two PHP scripts. 我试图用它来调用两个PHP脚本。 I found some examples online but just to call functions. 我在网上找到了一些示例,但仅用于调用函数。 I am just trying to call the scripts so it will load everything on my main PHP file that will then be display on the screen the results withouth refreshing the page. 我只是试图调用脚本,以便它将所有内容加载到我的主PHP文件中,然后将其显示在屏幕上,而无需刷新页面。

Here is the fiddle example, it works if I put all my PHP scripts in one file : http://jsfiddle.net/vw4w3ay5/ 这是小提琴示例,如果我将所有PHP脚本放在一个文件中,它将起作用: http : //jsfiddle.net/vw4w3ay5/

thanks in advance, your help is very much appreciated! 在此先感谢您的帮助!

main_php file (where I want to call my other PHP scripts): 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 (Script I want to call): 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(second script I want to call): 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?

?> ?>

What about? 关于什么? :

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

To be sure add $(document).ready(function(){ 确保添加$(document).ready(function(){

}); });

/ EDIT / Hum you want to use Ajax . / 编辑 /嗡嗡声,您想使用Ajax。 did you try with : 您尝试过:

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