-
Notifications
You must be signed in to change notification settings - Fork 0
YUI Ajax Helper
Category:Helpers | Category:Helpers::Ajax
This is a simple helper that extends the default form helper, and it adds a function to transform a simple form into an ajax one, it only needs the id of the form you want to upload, optionally you can give it a second paremeter, as if you dont specify this, it will send a POST request, you can change this to POST or GET. The third argument is an id of a div layer, if specified, the result of the request will go there, the fourth argument is a boolean value, if true it will make the div automatically, if false, it will assume you already have a form like that in your code, default is true.
[code]<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
if( ! function_exists('form_ajax')) { function form_ajax($form_id, $method = 'POST', $update = '', $create = true) { $automake_div = (!empty($update) && $create); $method = ($method === 'POST') ? 'POST' : 'GET'; return ($automake_div ? '
' : '').' [removed][removed] [removed] var doNotSubmit = function(e) { YAHOO.util.Event.preventDefault(e); makeRequest(); } YAHOO.util.Event.addListener(YAHOO.util.Selector.query("#'.$form_id.' input[type=submit]"), "click", doNotSubmit); '.($automake_div ? 'var div = document.getElementById("'.$update.'");' : '').'
var callback = {
success: function(o) {
if(o.responseText !== undefined) {
div[removed] = o.responseText;
}
}
}
var sUrl = document.getElementById("'.$form_id.'").action;
function makeRequest() {
YAHOO.util.Connect.setForm("'.$form_id.'");
YAHOO.util.Connect.asyncRequest("'.$method.'", sUrl, callback);
}
[removed]
';
}
}
/* End of MY_form_helper.php / / Location: ./system/application/helpers/MY_form_helper.php */[/code]
Here is a simple example
[code]<?php /**
- Ajax Test Controller */ class Test extends Controller { function index() { if($_POST) print_r($_POST); else { $this->load->helper('form'); echo form_open('test', array('id'=>'form')); echo form_input('title'); echo form_submit(array('name'=>'Submit', 'id'=>'Submit', 'value'=>'Submit this')); echo form_close(); echo form_ajax('form', 'POST', 'updateDiv', true); } } }
/* End of test.php / / Location: ./system/application/controllers/test.php */[/code]