handleAJAX()
public function handleAJAX() { //...your PHP code...// }
Provides you with way to handle AJAX without refreshing your entire Plugin.
You can make a call to this in JavaScript using the AJAX()
or JSON()
JavaScript functions.
If you are handling a request from the AJAX()
function then a reply is optional; i.e. you could just use an AJAX()
call to send a message from the client to the server without expecting a reply.
If you are handling a request from the JSON()
function then a reply is required. You should use PHP's json_encode()
function to ensure that your output is valid JSON.
Note that handleAJAX()
is not as guided as the other methods called by plugins, and is not linked to a Content Item or a Theme.
You should echo your any response you require to the output buffer. No return value is needed.
public function handleAJAX() { if ($this->post('upvote')) { $this->upvote($this->post('id')); } elseif ($this->post('downvote')) { $this->downvote($this->post('id')); } }
This code-fragment might form part of a voting system; it would allow a user to make a vote without refreshing or changing the Plugin on which they are on.