我是 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/