PDA

View Full Version : Save records to MySQL DB from Ext JS 4



james_D
28 Jun 2012, 10:03 AM
I need to know if there's a MVC tutorial (Video tutorial) that explains how to save some records to a MySQL database.

The view should be simple with 3-4 text fields, and i should save those records to the MySQL DB.

stimpy
28 Jun 2012, 10:45 AM
Not that I have seen .

Additonally connecting JS directly to a DB is terribly unsafe.

You need some serverside lanage ( Java, PHP) to handle the basics.

james_D
28 Jun 2012, 10:48 AM
Where could i start from. Is there a tutorial or a video tutorial i could start with ?

stimpy
28 Jun 2012, 10:51 AM
http://lmgtfy.com/?q=extjs+mvc+mysql+tutorial

You might try VIMEO if you really need a video

james_D
28 Jun 2012, 10:55 AM
I have tried google/youtube for tutorials, and since it didn't help me, I posted my question here. Thank you for your support.

stimpy
28 Jun 2012, 11:13 AM
James

Your question asked if there were any tutorials. That link results in many many tutorials with code samples .

What is it that you are looking for that you are not finding ?

Izhaki
29 Jun 2012, 2:02 AM
Ext Direct and Grid (http://docs.sencha.com/ext-js/4-1/#!/guide/direct_grid_pt1) demonstrates exactly that, step by step.

sword-it
29 Jun 2012, 5:34 AM
Hi,
You can save your formdata to the MySQL db by following steps:

1. Send the ajax request from your .js file to the server code which can be in php. Ajax function code is following:


// sending your form data as querystring
Ext.Ajax.request({
url: 'save-form.php?' + yourForm.getForm().getValues(true),
success: function(response){
// place ur code here that should run on success
}
});

2. Now you need a server script that can save ur data in MySQL database.The server file code of .php file:


<?php
//make database connection
mysql_connect("localhost", "YOUR USERNAME", "YOUR PASSWORD") or
die("Could not connect: " . mysql_error());
mysql_select_db("YOUR DATABASE");
// get ur form data fields values by their names like following:
$name = $_GET['name']; // getting value of form elements
$age = $_GET['age']; // getting value of form elements
$insertCmd ; // define insert command with the get values to this variable
$result = mysql_query($insertCmd);
echo $result; // true on success & false if values not inserted
}
?>

james_D
29 Jun 2012, 7:22 AM
What if i want to do a Select Statement with a Where clause. I am using POST and not GET


mysql_select_db("myDB", $con);
$res= mysql_query("SELECT * FROM Country WHERE city='London'");
$row = mysql_fetch_array($res);
echo $row;

I am using this tutorial (http://docs.sencha.com/ext-js/4-1/#!/api/Ext.form.Basic).

The failed block gets executed. How can i resolve this ?

el_chief
29 Jun 2012, 9:57 AM
James,

If you are doing this for real, then I would suggest using a PHP Framework (assuming you want to use PHP).

Symfony is probably the best, mostly due to its integration with Doctrine ORM.

Here's how it works:

ExtJS Grid has some new data in its store. You call store.sync().
The store uses its Writer to create JSON data from its in-memory Javascript data.
The store uses its Proxy to create an HTTP connection to your web server, and sends the JSON data there.
The web server starts up PHP and accepts the connection, and reads in the HTTP message.
You would parse the JSON and then load that data into your PHP Domain Object. You would want to do this so that you can validate your data in a structured manner. You don't want some yahoo injecting potentially harmful garbage into your database.
Then you'd use Doctrine to turn your Domain Object into SQL statements, saving it to the database.
Also, if you are just starting out, I highly recommend PostgreSQL over MySQL, as it has more features and is more standards-compliant.

As for a turnkey video, I am not sure, but I am sure that you can find them for the above parts.

Good luck!