LeBlaireau
LeBlaireau

Reputation: 17467

jquery ajax calling a method

I am using a class to do some CRUD stuff on a database, this one (http://net.tutsplus.com/tutorials/php/real-world-oop-with-php-and-mysql) I am going to use jquery to check the if the username has been registered already.

I could just create a php file specifically for that task, but would just like to extend the class and create a method callled checkname().

How can I call this in jquery?

Upvotes: 0

Views: 108

Answers (2)

user1056272
user1056272

Reputation:

You can use jQuery to make ajax call to a php file following:

PHP [suppose, 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

?>

jQuery:

$.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);
});

It is just an example.

Upvotes: 2

ceejayoz
ceejayoz

Reputation: 180147

You'll need to use jQuery's Ajax functionality to access a PHP page that calls that function. You can't directly call a PHP function via Ajax, something on the backend has to be set up.

Upvotes: 0

Related Questions