简体   繁体   中英

How can I call a PHP script using JQuery toggle and AJAX?

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.

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

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

<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 (script that I want to call):

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

?>

Three problems:

  1. In your PHP script you're treating a html string as though it was an object or array --- you cannot output a string literal as JSON

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

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

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

echo $html; 

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM