SISINDOTEK

I.T Solution | I.T Training | I.T Integration System

MyExtJS 4 Insert Update Delete with JSON PHP MySQL

view.js
C:

 Ext.onReady(function() { var cols = [
{ dataIndex: 'id', header: 'id', hidden: true },
{ dataIndex: 'title', header: 'Title', width: 200, editor: 'textfield'},
{ dataIndex: 'author', header: 'Author', width: 200, editor: 'textfield' },
{ dataIndex: 'isbn', header: 'ISBN', width: 100, editor: 'numberfield' },
], fields = []; for(var i=0; i<cols.length; i++) { fields.push(cols[i].dataIndex); } Ext.define('Book', { extend: 'Ext.data.Model', fields: fields }); var store = Ext.create('Ext.data.JsonStore', { model: 'Book', proxy: { type: 'ajax', url: 'view.php', reader: { type: 'json' } } }); Ext.create('Ext.grid.Panel', { columns: cols, width: 520, style: 'margin-top: 20%; margin-left: 35%', renderTo: Ext.getBody(), store: store }); store.load(); });

php code-(fill above grid form table record)

view.php
C:

 <?php require_once 'db.php'; $result = mysql_query('SELECT * FROM books'); while ($row = mysql_fetch_assoc($result)) { for ($i=0; $i < mysql_num_fields($result); $i++) { $meta = mysql_fetch_field($result, $i); } $rows[] = $row; } print (json_encode($rows)); ?> 

Simple Extjs form for add record into mysql table
edit.js-
C:

Ext.onReady(function(){ Ext.QuickTips.init(); var simpleForm = new Ext.FormPanel ({ labelWidth: 75, url:'edit.php', frame:true, title: 'Add book', bodyStyle:'padding:5px 5px 0', width: 350, defaults: {width: 230}, defaultType: 'textfield', items: [{
fieldLabel: 'Title',
name: 'title',
allowBlank:false
},{
fieldLabel: 'Author',
name: 'author'
},{
fieldLabel: 'ISBN',
name: 'isbn'
}], buttons: [{
text: 'Save',
handler: function () {
simpleForm.getForm().submit({
waitMsg: 'Saving...',
success: function () {
Ext.MessageBox.alert ('Message','Data has been saved');
simpleForm.getForm().reset();
},
failure: function () {
Ext.MessageBox.alert ('Message','Saving data failed');
}
});
}
},{
text: 'Cancel',
handler: function () {
simpleForm.getForm().reset();
}
}] }); simpleForm.render ('simple-form'); });

edit.php (for save record into table)
C:

<?php require_once 'db.php'; $q=mysql_query ("INSERT INTO books (title, author, isbn) VALUES (null, '".$_POST['title']."','".$_POST['author']."','".$_POST['isbn']."') ") or die ('{"success":"false"}'); // json output to notify the insert is success or not if ($q) { echo '{"success":"true"}'; } else { echo '{"success":"false"}'; } ?>

requoted by
SISINDOTEK – IT Training & Solution Provider
Jl. Sukasenang VI-6B Bandung – Jawa Barat 40124
Tel. 022-71242266 , SMS. 0812.8733.1966
info , YM. sisindotek
www.sisindotek.com , facebook.com/sisindotek , twitter.com/sisindotek

Please consider your environmental responsibility.
Before printing this e-mail message, ask yourself whether you really need a hard copy.

Single Post Navigation

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: