1. #1
    Sencha User
    Join Date
    Jun 2012
    Posts
    3
    Vote Rating
    0
    Nick334 is on a distinguished road

      0  

    Default Answered: Passing form values to PHP

    Answered: Passing form values to PHP


    in the following example when we click the button, all the form data (both the
    KEY-VALUE
    ) will be passed to the php file to be saved in the database. Grabbing the
    KEY-VALUE, I think this
    is done by
    var form = this.up('form').getForm();
    statement.


    Code:
    buttons: [{
            text: 'Submit',
            handler: function() {
                var form = this.up('form').getForm();
      
                if (form.isValid()) {
                    
                    form.submit({
                        success: function(form, action) {
                            Ext.Viewport.setActiveItem(1);
                           Ext.Msg.alert('Success', action.result.successmessage);
                           
                           Ext.create('Ext.container.Viewport', {
                               //layout: 'fit',
                               items: [
                               {
                                   xtype: 'newal'
                               }
                               ]
                           });
                        },
    What is the equivalent code of var form = this.up('form').getForm(); . I am using MVC, and my Model looks like;

    View:

    Code:
    this.buttons =[
                {
                    text:'Save',                   
                    id:'save',
                    name:'save',
                },...


    Store:

    Code:
    Ext.define('Game.store.myviewstore',{
        extend:'Ext.data.Store',
        model:'App.model.myviewstore',  
        proxy:{
            actionMethods :{
                create :'POST'
            },
            type:'ajax',
            url :'/savetodb.php'
        }   
    });


    I need to grab the Key-Value pair of the textfields in the View class and Pass it as parameters to the /savetodb.php file. So that the PHP will use it and save it to the database.

    But, i am unable to get the values of the textfields here. What have i done wrong and how could i make it correct ?

    note: if i hard code the PHP file with values, then it gets saved in the DB, but not the values that i passed from the VIEW of my application

  2. Nick:

    Sorry for the slow response. I was on holiday yesterday and have been busy. In case you haven't yet solved your issue, there is no reason for you to use a store just because you are using MVC. That same exact button handler code can be used. You would just add the click listener to the button using your controller. Depending on how you add the listener, your "this" will probably reference the controller. The first param of the listener is actually a reference to the button, so I would just use that parameter instead of "this" (or alternatively have a reference to the form defined in the refs so you don't need "this").

  3. #2
    Sencha User Tim Toady's Avatar
    Join Date
    Feb 2010
    Location
    Pennsylvania
    Posts
    570
    Vote Rating
    175
    Answers
    62
    Tim Toady is a splendid one to behold Tim Toady is a splendid one to behold Tim Toady is a splendid one to behold Tim Toady is a splendid one to behold Tim Toady is a splendid one to behold Tim Toady is a splendid one to behold Tim Toady is a splendid one to behold

      0  

    Default


    I am a bit confused by your thread. You are talking about submitting from a form, but then I see you have a store. The var form = this.up('form').getForm() does not get key value pairs. It grabs the basicform. The submit method is what actually creates and sends the key value pairs. If you just want to get the key value pairs and not submit, there is the getValues() method rather than submit.

    As far as the store is concerned I am not sure what you are trying to achieve. Is the store bound to a component? Will there be multiple records or are you still submitting from the form? When you are trying to sync the store, does it have any records in it?

  4. #3
    Sencha User
    Join Date
    Jun 2012
    Posts
    3
    Vote Rating
    0
    Nick334 is on a distinguished road

      0  

    Default


    Well what i actually need is to pass form data to PHP. I have created an MVC project.

    I found a code example from EXTJS 4 that does this without using a MVC structure. Here's the code for that;

    Code:
    buttons: [{         text: 'Submit',         handler: function() {             var form = this.up('form').getForm();               if (form.isValid()) {                                 form.submit({                     success: function(form, action) {                         Ext.Viewport.setActiveItem(1);                        Ext.Msg.alert('Success', action.result.successmessage);                                               Ext.create('Ext.container.Viewport', {                            //layout: 'fit',                            items: [                            {                                xtype: 'newal'                            }                            ]                        });                     },
    Since i am using MVC in my project i cann't use that code, therefore i am using STORES, the code for my Store class is as follows;

    Code:
    Ext.define('Game.store.myviewstore',{     extend:'Ext.data.Store',     model:'App.model.myviewstore',       proxy:{         actionMethods :{             create :'POST'},         type:'ajax',         url :'/savetodb.php'}});
    I am trying to pass all the values of the textfields found in my VIEW class to the URL /savetodb.php and i will save it in my DB. Using the above code i am unable to write to the DB using my PHP code.

    But i am able to READ records from the DB from that URL and STORE. I think i have not wrote the STORE class properly to WRITE to the DB. Therefore can you please help me sort this out.

    My PHP code is as follows; When i check firebug, under RESPONSE i see the echo statement 'worked' returned but the 'echo "$_POST[sometxtfield ]";' is empty. Why is this ? and how can i solve this ?

    Code:
    <?php
    
    mysql_connect("localhost","username","pwd");
    mysql_select_db("db");
    
    mysql_query("INSERT INTO tbl
    (name) VALUES('$_POST[sometxtfield]' ) ");
    
    echo "$_POST[sometxtfield ]";
    
    echo 'worked';
    ?>

  5. #4
    Sencha User
    Join Date
    Jun 2012
    Posts
    8
    Vote Rating
    0
    affiliateWise is on a distinguished road

      0  

    Default


    I am having a similar issue, I have a post the spells out what I am talking about if you would not mind looking at it. I am beating my head against the wall here trying to figure this out. Any aide is appreciated.
    http://www.sencha.com/forum/showthre...-php-variables

  6. #5
    Sencha - Support Team scottmartin's Avatar
    Join Date
    Jul 2010
    Location
    Houston, Tx
    Posts
    9,154
    Vote Rating
    475
    Answers
    685
    scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future

      0  

    Default


    Have look at the at the following online example:
    http://ext4all.com/post/extjs-4-mvc-...n-architecture

    You can check the headers on save to see info sent to server.

    Regards,
    Scott.

  7. #6
    Sencha - Support Team scottmartin's Avatar
    Join Date
    Jul 2010
    Location
    Houston, Tx
    Posts
    9,154
    Vote Rating
    475
    Answers
    685
    scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future

      0  

    Default


    Here is some quick for the server side: (sample, not based on example above)

    Code:
    //make database connection
    $connection = mysql_connect("localhost", $user, $pw) or
       die("Could not connect: " . mysql_error());
    mysql_select_db($db) or die("Could not select database");
    
    saveProduct();
    
    // -----------
    
    function getInputParms()
    {
        $result = NULL;
        if(function_exists('json_decode')) {
            $jsonData = json_decode(trim(file_get_contents('php://input')), true);
            $result = $jsonData['data'][0];
        }
        return $result;
    }
        
    function saveProduct() 
    {
        $jsonData = getInputParms();
    
        if (is_array($jsonData)) {
    
            if ($jsonData['idProduct'] > 0) {
                $id = $jsonData['idProduct'];
    
                $sql  = 'UPDATE products SET productName = "'.$jsonData['productName'].'",qty = '.$jsonData['qty'].',rate = '.$jsonData['rate'].',total = '.$jsonData['total'];
                $sql .= ' WHERE idProduct = '.$jsonData['idProduct'];
                $result = mysql_query($sql); // result set
            } else {
                $sql  = 'INSERT INTO products (productName,qty,rate,total) VALUES ("'.$jsonData['productName'].'",'.$jsonData['qty'].','.$jsonData['rate'].','.$jsonData['total'].')';
                $result = mysql_query($sql); // result set
                $id = mysql_insert_id();
            }
        }    
    
        $data = getProduct($id); // already encoded
    
        $return = array(
            'success' => TRUE,
            'data' => $data // this should be be data returned from new/updated record in table
        );
          
        $return = json_encode($return);
        echo $return;  
    }
    Scott.

  8. #7
    Sencha User Tim Toady's Avatar
    Join Date
    Feb 2010
    Location
    Pennsylvania
    Posts
    570
    Vote Rating
    175
    Answers
    62
    Tim Toady is a splendid one to behold Tim Toady is a splendid one to behold Tim Toady is a splendid one to behold Tim Toady is a splendid one to behold Tim Toady is a splendid one to behold Tim Toady is a splendid one to behold Tim Toady is a splendid one to behold

      0  

    Default


    Nick:

    Sorry for the slow response. I was on holiday yesterday and have been busy. In case you haven't yet solved your issue, there is no reason for you to use a store just because you are using MVC. That same exact button handler code can be used. You would just add the click listener to the button using your controller. Depending on how you add the listener, your "this" will probably reference the controller. The first param of the listener is actually a reference to the button, so I would just use that parameter instead of "this" (or alternatively have a reference to the form defined in the refs so you don't need "this").