How to make a remote connection with database using sencha touch

extjs, sencha-touch, sencha-touch-2

Solution

You can do it by making an `Ext.Ajax` request.

Let's assume that your form has 3 fields:-

- Name (`textfield`)

- Password (`passwordfield`)

- Age (`numberfield`)

You will get those fields values like shown below,

.....
.....
// form code ...
{
  xtype:'button',
  id:'submitBtn',
  text:'Submit',
  ui:'confirm',
  listeners : {
         tap : function() {
                var form = Ext.getCmp('form-id');
                var values = form.getValues();
                Ext.Ajax.request({
                      url: 'http://www.servername.com/insert.php',
                      params: values,

                      success: function(response){
                          var text = response.responseText;
                          Ext.Msg.alert('Success', text);
                     }

                     failure : function(response) {
                           Ext.Msg.alert('Error','Error while submitting the form');
                           console.log(response.responseText);
                     }
              });
         }
   }  
....
....

Now, at the server side, your `insert.php` code will make a connection with your database and insert the values & get the response back to the user.

<?php 
$con = mysql_connect("server","username","password");
mysql_select_db('database_name',$con);

$insertQry = "INSERT INTO tableName(name,password,age) VALUES ('".$_POST['name']."','".$_POST['password']."','".$_POST['age']."')";

if(mysql_query($insertQry))
{
    echo('success');
}
else 
{
    echo('failure' . mysql_error());
}
?>

Problem

How to make a remote connection with database using sencha touch I mean, how do you connect submitting the form to a remote database ? How do you get the response from database that your form has been submitted successfully ?

Original source