Articles: 843 | Categories: 148   
   
   
Home Articles Contact Us
 
 
 
 
Loading Data Into & Submitting a Form (0 Comments)
Admin: Posted Date: April 4, 2010

 We will go through the full circle of using forms, from populating the form with data from the server, to submitting that data back to the server. The backend I use is PHP and MySQL, however this tutorial is not specific to PHP and MySQL and only requires that you have a way to read and output JSON data on your server.

Loading Data Into & Submitting a Form

We will go through the full circle of using forms, from populating the form with data from the server, to submitting that data back to the server. The backend I use is PHP and MySQL, however this tutorial is not specific to PHP and MySQL and only requires that you have a way to read and output JSON data on your server.

Let's Get Started

We first need to set our form's url, which is a php script that will take POST data and write it to our database.

                          

  1. var form_employee = new Ext.form.Form({  
  2.     ...  
  3.     url:'forms-submit-process.php',  
  4.     ...  
  5. });  
var form_employee = new Ext.form.Form({ ... url:'forms-submit-process.php', ... });

Our data consist of five fields, id, name, title, hire_date and active, which will be retreived and placed into a data store.

The following code sets up the data store, at this point no data has been retrieved. Our proxy references a php script that retrieves row id 14 from our database and formats it as a JSON string.

  1. employee_data = new Ext.data.Store({  
  2.     proxy: new Ext.data.HttpProxy({url: 'forms-submit-getdata.php?id=14'}),  
  3.     reader: new Ext.data.JsonReader({},  
  4.         [ 'id''name''title''hire_date''active']  
  5.     ),  
  6.     remoteSort: false  
  7. });  
employee_data = new Ext.data.Store({ proxy: new Ext.data.HttpProxy({url: 'forms-submit-getdata.php?id=14'}), reader: new Ext.data.JsonReader({}, [ 'id', 'name', 'title', 'hire_date', 'active'] ), remoteSort: false });

The next thing to do is setup our event listener to watch for when the data loads, this ensures that we don't populate the form before the data has been loaded.

  1. employee_data.on('load'function() {  
  2.       
  3.     // data loaded, do something with it here...   
  4.               
  5. });  
employee_data.on('load', function() { // data loaded, do something with it here... });

When that data is loaded we can take it and populate the form fields using setValue. Here we are using getAt(0) to retrieve the first row of data (row zero) from our data store.

  1. employee_name.setValue(employee_data.getAt(0).data.name);  
  2. employee_title.setValue(employee_data.getAt(0).data.title);  
  3. employee_hire_date.setValue(employee_data.getAt(0).data.hire_date);  
  4. employee_active.setValue(  
  5.     Ext.util.Format.boolean(employee_data.getAt(0).data.active)  
  6. );  
employee_name.setValue(employee_data.getAt(0).data.name); employee_title.setValue(employee_data.getAt(0).data.title); employee_hire_date.setValue(employee_data.getAt(0).data.hire_date); employee_active.setValue( Ext.util.Format.boolean(employee_data.getAt(0).data.active) );

We will also want to create our submit button and render the form, remembering to set extra parameters to pass along with our POST data from the form fields. You will find it useful to pass a row identifier (id) so the php script knows which row to update in the database, along with an action just for good measure.

I'm also using isValid to make sure the form conforms to each field's requirements before submitting.

  1. form_employee.addButton('Save'function(){  
  2.     if (form_employee.isValid()) {  
  3.         form_employee.submit({  
  4.             params:{  
  5.                 action:'submit',  
  6.                 id:employee_data.getAt(0).data.id  
  7.             },   
  8.             waitMsg:'Saving...'  
  9.         });   
  10.     }else{  
  11.         Ext.MessageBox.alert('Errors''Please fix the errors noted.');  
  12.     }  
  13. }, form_employee);  
  14.   
  15. form_employee.render('employee-form');  
form_employee.addButton('Save', function(){ if (form_employee.isValid()) { form_employee.submit({ params:{ action:'submit', id:employee_data.getAt(0).data.id }, waitMsg:'Saving...' }); }else{ Ext.MessageBox.alert('Errors', 'Please fix the errors noted.'); } }, form_employee); form_employee.render('employee-form');

Loading Our Data

                          

Now we load the data.

  1. employee_data.load();  
employee_data.load();

It really can be just that easy to create a working Ext form with an amazing level of usability right out of the box. We have some server side scripts that retrieve and format data from our database, and write that data back to our database. These server side scripts can be as simple as a few lines.

 

 

 
 
Add a Comment:
 
(You must be signed in to comment on an article. Not a member? Click here to register)
   
Title:

Comments: