Skip to content

Instantly share code, notes, and snippets.

@thetristan
Created June 22, 2011 18:05
Show Gist options
  • Save thetristan/1040707 to your computer and use it in GitHub Desktop.
Save thetristan/1040707 to your computer and use it in GitHub Desktop.
AJAX in modules
//code to set up an action in my upd.module.php
$this->EE->load->dbforge();
$data = array(
'class' => 'Module',
'method' => 'api_get_hash',
);
$this->EE->db->insert('actions', $data);
//this class method gets loaded as part of a helper and used elsewhere in my code...
/**
* End the request and push out any output and encode
* as needed.
*
* @param mixed $output
* @param bool $encode
* @return void
* @author Tristan Blease
*/
public static function end_request( $output=null, $encode=false ) {
if ( !is_null($output) )
{
if ( $encode )
$output = json_encode( $output );
exit( $output );
}
}
//then for ajax requests I've got an action set up in the module that routes to this class method
/**
* Get a token for CSRF protection
*
* @return void
* @author Tristan Blease
*/
public function api_get_hash() {
Helper::end_request( $this->EE->functions->add_form_security_hash('{XID_HASH}') );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment