Grid PHP SQL: Learn Ext JS Form [ Adding a new entry ]
Submitted by tuan.norlida on Tue, 06/29/2010 - 10:57
We will build a new window that willcontain a form for filling out name and ic number as samples of data.
Here you go the steps;
File personal.js
1.Define the form and window:
- var PersonalCreateForm;
- var PersonalCreateWindow;
2. Define the field:
- var NameField;
- var ICField;
3. Buid code to describe behavior:
- NameField = new Ext.form.TextField({
- id: NameField,
- fieldLabel: 'Name',
- maxLength: 20,
- allowBlank: false,
- anchor : '95%',
- maskRe: /([a-zA-Z0-9\s]+)$/
- });
- ICField = new Ext.form.NumberField({
- id: ICField,
- fieldLabel: 'IC Number',
- maxLength: 20,
- allowBlank: false,
- anchor : '95%',
- maskRe: /([a-zA-Z0-9\s]+)$/
- });
4. Create the form:
- PersonalCreateForm = new Ext.FormPanel({
- labelAlign: 'top',
- bodyStyle:'padding:5px',
- width: 600,
- items: [{
- layout:'column',
- border:false,
- items:[{
- columnWidth:0.5,
- layout: 'form',
- border:false,
- items: [NameField, ICField]
- }],
- buttons: [{
- text: 'Save and Close',
- handler: createThePersonal
- },{
- text: 'Cancel',
- handler: function(){
- // because of the global vars, we can only
- // instantiate one window... so let's just hide it.
- PersonalCreateWindow.hide();
- }
- }]
- });
- PersonalCreateWindow= new Ext.Window({
- id: 'PersonalCreateWindow',
- title: 'Creating a New Personal',
- closable:true,
- width: 610,
- height: 250,
- plain:true,
- layout: 'fit',
- items: PersonalCreateForm
- });
5. Let's set up some small functions to work with our form.
- // reset the Form before opening it
- function resetPersonalForm(){
- NameField.setValue('');
- ICField.setValue('');
- }
- // check if the form is valid
- function isPersonalFormValid(){
- return(NameField.isValid()&& ICField.isValid();
- }
- // display or bring forth the form
- function displayFormWindow(){
- if(!PersonalCreateWindow.isVisible()){
- resetPersonalForm();
- PersonalCreateWindow.show();
- } else {
- PersonalCreateWindow.toFront();
- }
- }
6. Implement a save method.
- function createThePersonal(){
- if(isPersonalFormValid()){
- Ext.Ajax.request({
- waitMsg: 'Please wait...',
- url: 'personal.php',
- params: {
- task: "CREATE",
- name: NameField.getValue(),
- ic: ICField.getValue()
- },
- success: function(response){
- var result=eval(response.responseText);
- switch(result){
- case 1:
- Ext.MessageBox.alert('Creation OK',
- 'The Personal was created successfully.');
- PersonalsDataStore.reload();
- PersonalCreateWindow.hide();
- break;
- default:
- Ext.MessageBox.alert('Warning',
- 'Could not create the Personal.');
- break;
- }
- },
- failure: function(response){
- var result=response.responseText;
- Ext.MessageBox.alert('error',
- 'could not connect to the database.
- retry later');
- }
- });
- } else {
- Ext.MessageBox.alert('Warning',
- 'Your Form is not valid!');
- }
- }
7. We simplyneed to add this to our EditorGrid constructor :
- tbar: [{
- text: 'Add a Personal',
- tooltip: 'Great Tooltip',
- iconCls:'add', // this is defined in our styles.css
- handler: displayFormWindow
- }]
File personal.php
8. Finally;
First the switch:
- case "CREATE":
- createPersonal();
- break;
9. And then thefunction:
- function createPersonal(){
- $name = addslashes($_POST['name']);
- $ic = addslashes($_POST['ic']);
- // Here we should probably do some database checking,
- // to make sure that we do not have the same entry
- // twice for ex... And we would return a different
- // error code (ECHO '0' or whatever you want...)
- // For now we'll pretend like the entry is valid.
- $query = "INSERT INTO Personals (`IDPersonal`,`Name`,`IC`))
- VALUES (NULL , '$name', '$ic ')";
- $result = mysql_query($query);
- echo '1';
- }
10. Done!
Ref: http://www.sencha.com/learn/Tutorial:Grid_PHP_SQL_Part4
- tuan.norlida's blog
- Login to post comments
Copyright © 2008-2011
nice extjs javascript tutorial
nice and simple and useful extjs javascript tutorial