繁体   English   中英

为.getJSON定义特定的功能

[英]Defining a specific function for .getJSON

我正在尝试使用Json通过调用function.php来从数据库获取数据来查询数据库。 我在寻找如何在function.php文件中指定特定功能来执行工作时遇到了一个问题。

下面是我的Jquery和function.php,目标是让Json使用来自function.php的getlist()。

请帮助我一段时间以来一直试图理解这一点。

jQuery函数:

$.getJSON( "public/includes/functions.php", function( data ) {
        var items = [];
      $.each( data, function( key, val ) {
        console.log('data:' + key + ' And ' + val);
      });

     });

PHP功能:

function getlist(){

    require "dbconn.php";
    $result = $stdb->get_results("SELECT id, name FROM supplements");
    $stdb->show_errors();


    if (mysqli_num_rows($result) > 0) {
    // output data of each row
        while($row = mysqli_fetch_assoc($result)) {

         $array[] = $row;

        }
    } else {
        echo "0 results";
    }
    $js_array = json_encode($array);
    echo $js_array;

    mysqli_close($conn);    
}
$.getJSON( "public/includes/functions.php?myfun=1", function( data ) {
        var items = [];
      $.each( data, function( key, val ) {
        console.log('data:' + key + ' And ' + val);
      });

     });



    <?php 
    if(isset($_GET)){
        if(isset($_GET['myfun'])){
        getlist();exit;
}
    }
    function getlist(){

        require "dbconn.php";
        $result = $stdb->get_results("SELECT id, name FROM supplements");
        $stdb->show_errors();


        if (mysqli_num_rows($result) > 0) {
        // output data of each row
            while($row = mysqli_fetch_assoc($result)) {

             $array[] = $row;

            }
        } else {
            echo "0 results";
        }
        $js_array = json_encode($array);
        echo $js_array;

        mysqli_close($conn);    
    }
    ?>
function getlist(){

    require "dbconn.php";
    $result = $stdb->get_results("SELECT id, name FROM supplements");
    $stdb->show_errors();


    if (mysqli_num_rows($result) > 0) {
    // output data of each row
        while($row = mysqli_fetch_assoc($result)) {

         $array[] = $row;

        }
    } else {
        echo "0 results";
    }
    $js_array = json_encode($array);
    echo $js_array;

    mysqli_close($conn);    
}
//You need to call your function
getlist();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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