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

标签 php jquery html ajax mysqli

我是 AJAX 和 JQuery 的新手。我试图用它来调用两个 PHP 脚本。我在网上找到了一些示例,但只是为了调用函数。我只是想调用脚本,以便它将所有内容加载到我的主 PHP 文件中,然后在不刷新页面的情况下将结果显示在屏幕上。

这是 fiddle 示例,如果我将所有 PHP 脚本放在一个文件中,它就可以工作:http://jsfiddle.net/vw4w3ay5/

提前致谢,非常感谢您的帮助!

ma​​in_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*/
});

关于php - 如何使用 AJAX/JQuery 调用多个 PHP 脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26260760/

相关文章:

php - 如何更新与其当前状态相对应的列

php - 将 php 结果作为内容保存在 div 中

jquery - elem.nodeName 返回未定义,jQuery 中的错误?

html - CSS:不需要 div 额外边距

javascript - 使用 FormData 上传 AJAX 图片不在服务器端显示图片

解析来自 xml 的数组时出现 PHP 错误

javascript - Jquery 切换和关闭单击

javascript - 我需要找到表中一列的总值,不包括最后一行

javascript - 可见文本的正则表达式,而不是 HTML

javascript - 将下拉列表中选定的文本值显示到按钮