简体   繁体   中英

How to get the return value of a php function when calling from jQuery?

I have a php function return a string value which will put into html file.

function getDirectionInfo($routeNumber) {
    //some code here
    $dirinfo = "<p> some text </p>";
    return $dirinfo;
}

if (isset($_POST['getDirectionInfo'])) {
    getDirectionInfo($_POST['getDirectionInfo']);
}

So in jQuery, I have a following function

$(".onebtn").click(function(){
    $("#directioninfo").empty();
    var routeNumber = $(this).text();
    $.ajax({
        url: "./systemView_Function.php",
        type: "POST",
        data: {"getDirectionInfo": routeNumber},
        success: function(data) {   
            console.log("HIHIHIHI");
            $("#directioninfo").append(data);
        }
    });
})

Now console.log prints the "HIHIHIHIHI", but jQuery does not append the data to html. Anyone know how to get the return value of php function when calling from jQuery?

Instead of return use:

echo json_encode($dirinfo);
die;

It's also good idea to add dataType field to your $.ajax() function params set to json , to make sure, that data in your success function will be properly parsed.

  1. You just need to send the response back using echo

  2. Use var routeNumber = $(this).val(); to get the button value

PHP:

<?php
function getDirectionInfo($routeNumber) {
    //some code here
    $dirinfo = "<p> routeNumber". $routeNumber." </p>";
    return $dirinfo;
}

if (isset($_POST['getDirectionInfo'])) {
    echo getDirectionInfo($_POST['getDirectionInfo']);
}else{
    echo "not set";
}

AJAX & HTML

 <!DOCTYPE html> <html> <head> <script src="http://code.jquery.com/jquery-latest.js"></script> <script> $(document).ready(function(){ $(".onebtn").click(function(){ $("#directioninfo").empty(); var routeNumber = $(this).val(); console.log("routeNumber = " + routeNumber); $.ajax({ url: "systemView_Function.php", type: "POST", data: {"getDirectionInfo": routeNumber}, success: function(data) { console.log("data = " + data); $("#directioninfo").append(data); } }); }) }); </script> </head> <body> <div id="directioninfo"></div> <input type="button" value="12346" class="onebtn" /> </body> </html> 

Thank you for everyone. I have just found that I made a very stupid mistake in jQuery. I should use var routeNumber = parseInt($(this).text()); instead of var routeNumber = $(this).text(); So the following code work to get the return value of php function when calling from jQuery.

in php

function getDirectionInfo($routeNumber) {
    //some code here
    $dirinfo = "<p> some text </p>";
    echo json_encode($dirinfo);
}

if (isset($_POST['getDirectionInfo'])) {
    getDirectionInfo($_POST['getDirectionInfo']);
}

in jQuery

$(".onebtn").click(function(){
    $("#directioninfo").empty();
    var routeNumber = parseInt($(this).text());
    $.ajax({
        url: "./systemView_Function.php",
        type: "POST",
        data: {"getDirectionInfo": routeNumber},
        dataType: "JSON",
        success: function(data) {   
            $("#directioninfo").append(data);
        }
    });
})

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