1. #1
    Sencha User
    Join Date
    Mar 2012
    Posts
    23
    Vote Rating
    0
    ataker98 is on a distinguished road

      0  

    Default Answered: How to connect sencha touch with mysql?

    Answered: How to connect sencha touch with mysql?


    Hi, I want know how to connect my app to mysql data base, I made this a form page with these fields:
    Code:
    xtype: 'fieldset',
    items: [
    {
    xtype: 'textfield', name : 'name', label: 'Name' }, { xtype: 'emailfield', name : 'email', label: 'Email' }, { xtype: 'passwordfield', name : 'password', label: 'Password' } ]
    and my doubt is:
    do I have to create a php file to connect my data base and a Model with AJAX proxy to take data written in my form's fields and save in my data base? Do I have to use Store too?

  2. The common way is to have a web service that will take care of the CRUD. So yes you need the php script you mentioned.
    You don't HAVE TO have a model, store, proxy, reader but it is easier and nicer to use them, that;s why we use the framework, to make use of its tools

    For your case you could have:
    - a User model to reflect form fields as in name, password, email
    - the model can specify a proxy that can be Ajax or Rest
    - on form submit you can create a new User model instance, populate it with form's data, and after that use model.save()

    No need of a store just for saving data. But if you will want a list of users, then you will surely want to use store


    http://docs.sencha.com/touch/2-0/#!/api/Ext.data.Model-method-save

    http://docs.sencha.com/touch/2-0/#!/...re-method-sync

  3. #2
    Sencha User
    Join Date
    Mar 2012
    Posts
    18
    Vote Rating
    1
    Answers
    2
    cchilds is on a distinguished road

      0  

    Default


    Do you mean you have a mysql database on a server that you want to connect to? In that case, you will need to have a way to access it (through PHP for example) from the server. If you are simply trying to save data locally, you can create and use your own local sqlite database. I personally use PhoneGap and its Storage API.

  4. #3
    Sencha User bluehipy's Avatar
    Join Date
    Mar 2010
    Location
    Romania
    Posts
    618
    Vote Rating
    29
    Answers
    67
    bluehipy will become famous soon enough bluehipy will become famous soon enough

      0  

    Default


    The common way is to have a web service that will take care of the CRUD. So yes you need the php script you mentioned.
    You don't HAVE TO have a model, store, proxy, reader but it is easier and nicer to use them, that;s why we use the framework, to make use of its tools

    For your case you could have:
    - a User model to reflect form fields as in name, password, email
    - the model can specify a proxy that can be Ajax or Rest
    - on form submit you can create a new User model instance, populate it with form's data, and after that use model.save()

    No need of a store just for saving data. But if you will want a list of users, then you will surely want to use store


    http://docs.sencha.com/touch/2-0/#!/api/Ext.data.Model-method-save

    http://docs.sencha.com/touch/2-0/#!/...re-method-sync

  5. #4
    Sencha User
    Join Date
    Mar 2012
    Posts
    23
    Vote Rating
    0
    ataker98 is on a distinguished road

      0  

    Default


    thanks for answer, I tried to implement based what you said, I'm created a simple form with one field (name) with a button when it is clicked save the name in my database called user.
    but I'm having difficult and I don't know if I understood correctly, this model.save() can put it in controller?
    I created a php file called connect.php that connects to mysql database, before use model.save() I have to use connect.php to connect database isn't it?
    I'm very confused.

  6. #5

  7. #6
    Sencha User
    Join Date
    Mar 2012
    Posts
    23
    Vote Rating
    0
    ataker98 is on a distinguished road

      0  

    Default


    Thank you for reply, I think I'm getting closer to do to the connection, but I'm receiveing this error:
    POST http://localhost/php/user.php?_dc=1343506774213 404 (Not Found)

    I created this code for Model:

    Code:
    fields: [
       {name: 'name', type: 'string'},
       {name: 'email', type: 'string'},
       {name: 'password', type: 'string'}
      ],
      proxy: {
       type: 'rest',
       url: '/php/user.php',
      }
    then in Controller when click on button:

    Code:
    saveData: function(){
      var user = Ext.create('Book.model.User',{
       name: 'C',
       email: 'email',
       password: '123356'
      });
      user.save();
        },
    In php I made this:

    PHP Code:
    <?PHP
     
    include("conect.php");
     
     
    $n $_POST['name'];
     
    $e $_POST['email'];
     
    $s $_POST['password'];
     
    $action $_SERVER['REQUEST_METHOD'];

     if(
    $action == 'GET') {
     
     } else if(
    $action == 'POST') {
      
    mysql_query("INSERT INTO user (name, email, password)
      VALUES ('
    $n', '$e', '$s')");
      
     } else if(
    $action == 'PUT') {

       } else if(
    $action == 'DELETE') {
     }
    ?>
    could you tell me what is wrong?

  8. #7
    Sencha User bluehipy's Avatar
    Join Date
    Mar 2010
    Location
    Romania
    Posts
    618
    Vote Rating
    29
    Answers
    67
    bluehipy will become famous soon enough bluehipy will become famous soon enough

      0  

    Default


    Yes, probably the url to your php script.

    If you navigate in yur browser to http://localhost/php/user.php what do you see?

  9. #8
    Sencha User
    Join Date
    Mar 2012
    Posts
    23
    Vote Rating
    0
    ataker98 is on a distinguished road

      0  

    Default


    Now it works, I used form submit that you said:
    Code:
    var form = this.getCadForm(),
       usuario = Ext.create('Book.model.Usuario',{
       nome: 'C',
       email: 'email',
       senha: '123456'
      });
      form.setRecord(user);
      
      form.submit({
       url: 'php/user.php',
       method: 'POST',
       success: function() {
        alert('form submitted successfully!');
       }
      });
    Now I can see data in phpmyadmin, but I'm receiving this error:
    Uncaught Error: You're trying to decode an invalid JSON String:]
    Do you know what it means?

  10. #9
    Sencha User bluehipy's Avatar
    Join Date
    Mar 2010
    Location
    Romania
    Posts
    618
    Vote Rating
    29
    Answers
    67
    bluehipy will become famous soon enough bluehipy will become famous soon enough

      0  

    Default


    Yes, probably you respond with something that is not json encoded.
    Normally you should respond withthe same record + the id your db server is setting to it. But just for now you can reflect the object you send. Don't forget to use json_encode in php.

  11. #10
    Sencha User
    Join Date
    Mar 2012
    Posts
    23
    Vote Rating
    0
    ataker98 is on a distinguished road

      0  

    Default


    I got it, thank you for replying my questions