我如何可以调用使用JQuery切换和AJAX PHP脚本? [英] How can I call a PHP script using JQuery toggle and AJAX?

查看:84
本文介绍了我如何可以调用使用JQuery切换和AJAX PHP脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打电话给我的主PHP文件的PHP脚本。我想对正在运行的SQL查询显示从我的PHP脚本的结果。

I am trying to call a PHP script in my main PHP file . I want to display the results from my PHP script with the SQL queries that are being run.

我还想包括显示结果动态/通过不刷新页面的可能性。

I'd also like to include the possibility of showing the results dynamically/by not refreshing the page.

这是我试过到目前为止,即时通讯新jQuery和AJAX。在此先感谢!

this is what I tried so far, im new to Jquery and AJAX. thanks in advance!

工作拨弄 http://jsfiddle.net/52n861ee/ 这就是我想做的事情,但是当我点击它会告诉我的错误:第23行(在这里我使用json_en code

working fiddle: http://jsfiddle.net/52n861ee/ thats what I want to do but when I click on it will tell me error: line 23 (" where I am using json_encode

的JQuery / AJAX部分:

<div id="map_size" align="center">
<script type="text/javascript">
                    //Display station information in a hidden DIV that is toggled
                    //And call the php script that queries and returns the results LIVE
                    $(document).ready(function() {
                    $(".desk_box").click(function() {
                        $id = $(this).attr("data")
                    $("#station_info_"+$id).toggle();

                    $.ajax({
                        url:"display_stationinfo.php",
                        type: "GET",
                        success:function(result){
                    $("#station_info_"+$id).html(result);
                    }});//end ajax  
                    });//end click
                    });//end ready
    </script>
</div> <!-- end map_size -->

display_station.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
$html = "<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
    echo $_GET['callback'] . '(' .json_encode($html) . ')';             
mysqli_close($conn); // <-- DO I NEED TO INCLUDE IT HERE OR IN MY db_conn.php SINCE IM INCLUDING IT AT THE TOP?

?>

推荐答案

三个问题:

  1. 在你的PHP脚本你治疗HTML字符串,就好像它是一个对象或数组--- 您不能输出一个字符串作为JSON

在你的JS脚本你治疗JSON[对象],就好像它是一个字符串或HTML对象--- 您不能输出JSON这样,你需要使用DOT符号来访问它的数据

In your JS script you're treating the 'JSON' [object] as though it was a string literal or a html object --- you cannot output JSON that way, you need to use DOT notation to access it's data.

即使你的PHP脚本似乎发送 JSONP 您还没有指定一个 JSONP 的dataType AJAX 电话。

Even though your PHP script seems to be sending JSONP you have not specified a jsonp dataType in your ajax call.

假设你所有的PHP脚本都在同一个域,将回声$ _GET ['回调']。 (.json_en code($ HTML)。); 来:

Assuming all your PHP scripts are on the same domain, change echo $_GET['callback'] . '(' .json_encode($html) . ')'; to:

echo $html; 

让你的PHP输出与您的JS所预期的一致。

So that your PHP output is consistent with what your JS expects.

这篇关于我如何可以调用使用JQuery切换和AJAX PHP脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆