AJAX request and PHP class functions

后端 未结 8 860
小鲜肉
小鲜肉 2020-12-08 05:29

How to call a PHP class function from an ajax call

animal.php file

class animal
{     
  function getName()
  {
    return \"lion\";
  }         


        
8条回答
  •  太阳男子
    2020-12-08 05:35

    OOP Currently with php:

    ajax.html program(client tier) -> program.php (middle tier) -> class.php (middle tier) -> SQL call or SP (db tier)

    OOP Currently with DotNet:

    ajax.html program(client tier) -> program.aspx.vb (middle tier) -> class.cls (middle tier) -> SQL call or SP (db tier)

    My real-life solution: Do OOA, do not OOP.

    So, I have one file per table -as a class- with their proper ajax calls, and select the respective ajax call with a POST parameter (i.e. mode).

    /* mytable.php */

    "; // colud be a json, html
        };  
    };
    
    //json
    if($_POST["mode"]=="json_a")    {
        $cadena="select * from mytable where name like '%".$_POST['txtmytablename']."%'"; 
        $rs=mysql_query($cadena,$cn) or die(mysql_error().' : '.$cadena); 
        $result = array();
        while ($row=mysql_fetch_array($rs)) {
            array_push($result, array("id"=>$row['code'],"value" => $row['name']));
        };  
        echo json_encode($result);
    };
    ?>
    

提交回复
热议问题