Php jqueryajax调用方法

Php jqueryajax调用方法,php,jquery,ajax,Php,Jquery,Ajax,我用一个类在数据库上做一些CRUD的东西,这一个(http://net.tutsplus.com/tutorials/php/real-world-oop-with-php-and-mysql)我将使用jquery检查用户名是否已经注册 我可以专门为该任务创建一个php文件,但只想扩展该类并创建一个名为checkname()的方法 如何在jquery中调用它?您需要使用jquery的Ajax功能来访问调用该函数的PHP页面。您不能通过Ajax直接调用PHP函数,必须在后端设置一些东西。您可以使用

我用一个类在数据库上做一些CRUD的东西,这一个(http://net.tutsplus.com/tutorials/php/real-world-oop-with-php-and-mysql)我将使用jquery检查用户名是否已经注册

我可以专门为该任务创建一个php文件,但只想扩展该类并创建一个名为checkname()的方法


如何在jquery中调用它?

您需要使用jquery的Ajax功能来访问调用该函数的PHP页面。您不能通过Ajax直接调用PHP函数,必须在后端设置一些东西。

您可以使用jQuery对PHP文件进行Ajax调用,如下所示:

PHP[假设,test.PHP]

<?php

class ABC extends XYZ {
  public function checkname() {
    if(isset($_POST) && !empty($_POST['name'])) {
      echo json_encode(array('status' => 'done'));
    }
  }
}
$ins = new ABC();
$ins->checkname(); // calling the function checkname

?>

这只是一个例子。

为什么不做一个查询来检查用户名是否已经在数据库中?如果不是->创建等。我有时喜欢net tuts+,但那个教程很可怕。我需要的信息是它不能直接调用。我可以传递一些数据并使用if或switch语句调用它。
$.ajax({
  url: 'test.php', //  write the url correctly
  type: 'post',
  data: "name=XYZ&location=PQR"
}).done(function(response) {
   console.log(response.status); // will log done
}).fail(function(jqXHR, textStatus) {
   console.log("Failed: " + textStatus);
});