javascript - 如何使用ajax调用php类方法

标签 javascript php jquery ajax

我是 ajax 新手。我试图在表单文本框更改事件中通过 ajax 调用 get_mother 方法。我想在数据列表中显示结果。下面是我使用过的代码。

class ChildApplication extends Application{
function __construct(){
    $this->login_required();
}
function get_mother(){
    $mother = $_POST['mother_name'];
    $mother = '%'.$mother.'%';
    $db=$this->get_dbo();
    $sql = "SELECT * FROM tbl_mother WHERE `mother_fname` LIKE ? ";
    $results = $db->load_result($sql,array($mother));
    return $results;
}
function get_child($mother){
 //statements 
}
}

我的脚本是:

   $(document).ready(function(){
     $("#mother_name").keyup(function(event){
        event.preventDefault();
         var mother = $("#mother_name").val();
         $.ajax({
            type: 'POST',
            url: 'applications/child/child.php',
            data: dataString,
            dataType: 'json',
            success: function(){
                alert("pass");
            },
            error: function(){
                alert("error");
            }
            });

           });
         });

没有显示任何警报。请帮我解决问题

最佳答案

我猜想“dataString”变量没有定义。 我认为你应该像这样替换“data”的值:

data: {mother_name: mother},

还要确保函数 get_mother() 在“applications/child/child.php”中被调用

$ChildApplication = new ChildApplication;
$ChildApplication->get_mother();

关于javascript - 如何使用ajax调用php类方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37255194/

相关文章:

javascript - 如何将div的内容保存为图像?

javascript - getUsermedia() 前置摄像头错误错误 : "Notreadableerror: could not start video source"

javascript - 如何自定义字段类型验证的默认浏览器错误消息?

php - 使用 Memcache 构建 PHP/Javascript 聊天室

javascript - 在 JQuery - CSS 中显示隐藏问题

javascript - 如何使用 Selenium 中的 JQuery 在 h3 中创建 .delay 函数?

javascript - 如何管理表和javascript中的输入

php - 多表SQL交叉引用搜索困难

php - LAMP mcrypt 扩展

javascript - 通过 Jquery attr.() 过滤链接数组