如何调用使用Ajax PHP函数?函数、Ajax、PHP

2023-09-10 14:43:05 作者:初相识

我有一个PHP函数返回一个数组:

I have a PHP function which returns an array :

function lire( $id) {
            $ret = array() ; 
            $sSQL = "SELECT  *  FROM produit WHERE prod_code = '$id' LIMIT 1" ;
            $this->db->query($sSQL) ;
            $ret['cnt'] = $this->db->num_rows() ;
                        return $ret;
}

我怎么能调用这个PHP函数中的Ajax?

How can I call this PHP function within Ajax ?

推荐答案

你必须把这个code块在一个PHP文件,将不得不调用它像AJAX功能:

You have to put this code block in a php file and will have to call it in the ajax function like:

    var value = $.ajax({
    type: "POST",
    url: "some.php",
    data: "name="+customvariable,
    async: false
    }).responseText;

也some.php文件将有

also the some.php file will have

      if(isset($_POST['name']))
       {
        $id = mysql_real_escape_string($_POST['name']);
        $ret = array() ; 
        $sSQL = "SELECT  *  FROM produit WHERE prod_code = '$id' LIMIT 1" ;
        $this->db->query($sSQL) ;
        $ret['cnt'] = $this->db->num_rows() ;
                    echo $ret;
      }

您将获得的返回值在值变量。

You will get the return value in the value variable.