Ajax saving
Ajax saving in Tequila is a straightforward action!
The real fast way
Ajax saving and data retrieval is actually included in any generated page, you will find 2 buttons, one call save and continue who use Ajax and a normal submit button.
Nothing else required.
The extended way
Naturally you are not always working on generated pages so how to implement Ajax saving on Tequila?
*Remember if you are working on a class that inherits application_controller_crud the methods for Ajax are already inherited
First let's review what steps are included in Ajax saving:
Pick all values from form
Submit the form to an URL via Post or Get
Process the form on server and return an XML message
Display a successful result or modify your screen somehow
* Some people like returning Text or JSON instead of XML, Tequila allow you any of them.
Required resources
Create the form you want to save
Add a save ajax button
Include the Javascript Ajax libraries
* This tutorial uses the Ajax library included with Tequila (self made) but you can replace for your favorite one
Step 1. Adding Javascript libraries
To add any javascript library in Tequila you just call addJS function:
addJS('ajax.js');
* You don't need to modify your template (that will reduce versatility of your code)
Step 2. Javascript side
Tequila ajax library offer many ways to facilitate your work. The easiest way to do this is by calling the function:
url = "index.php?task=yourtask&mode=yourmode&rrt=xml saveformajax(myform, "url", "id_holder");
* There are functions to save also forms that use RTE controls, FCK and nicEdit (nicEdit is currently the official we like :P)
That's it!, this function will:
Perform validation (by calling the validateonSubmit function if exist)
Collect any fields in the form
Send the values to the specified 'url'
Set the returned 'id' in id_holder field so next saves are updates instead of inserts
Alternative code. If you prefer to do this by yourself you will code something like
function mysaveajax(){ data = getFormValues(myform); var ajax = new AJAXInteraction (url, confirmsaveajax, id_holder); ajax.doPost(data);}
Step 3. PHP Side
On php we will process and save the data
In this example we will save employee information.
1 //controller 2 protected function mysaveajax(&$model, &$view) 3 { 4 $sentid = safePost('id'); //You id field here 5 $emp = new employee_VO(); 6 $this->readpost($emp); // Fill the object from post 7 $status = $model->save($emp, $sentid); 8 $view->show_mysave($emp, $status, true); 9 } 10 // model 11 public function save($employee, $dbid) 12 { 13 $dao = new employee_DAO(); 14 $dao->save($employee, $dbid); 15 } 16 // View 17 public function show_mysave($employee, $status) 18 { 19 $data = null; 20 $data->status = $status; 21 $data->id = $employee->id; 22 $st = new view_xml_status($this->template, $data); 23 // echo only when not using templates 24 echo $st->getview(); 25 }
Step 4. Updating client
Nothing to do here, the save function on Javascript will take care for us! The ajax library will also show a - saving - message or a waiting image if you define in the library configuration!