Results 1 to 6 of 6

Thread: data store fed from php not working

  1. #1

    Default data store fed from php not working

    I am trying to implement a simple data store that gets its data from a php backen:

    Code:
            data = new Ext.data.Store(
                {
                  id: 'data',
                  proxy: new Ext.data.HttpProxy({
                      url: 'data.php',      // File to connect to
                      method: 'POST'
                  }),
                  baseParams:{
                      task: "list"
                  }, // this parameter asks for listing
                  reader: new Ext.data.JsonReader(
                      {
                          root: 'results',
                            totalProperty: 'total',
                            id: 'id'
                        },[
                            {
                                name: "id",
                                type: "int",
                                mapping: "id"
                            }
                        ]
                    )
                }
    then I do
    Code:
            data.load(
                {
                    callback: onDataLoad
                }
            );
    and on the php side i do something as simple as:

    Code:
    <?php
        $mysql = mysql_connect("localhost", "root", "root");
        if (!$mysql) {
            $error = "Could not connect: " . mysql_error();
            goto finish;
        }
        if (!mysql_select_db("blocktrain", $mysql)) {
            $error = "mysql_select_db failed: " . mysql_error();
            goto finish;
        }
        $result = mysql_query("select id from test", $mysql);
        if (!$result) {
            $error = "mysql_query failed: " . mysql_error();
            goto finish;
        }
    
        $arr = array();
        $nbrows = 0;
        while ($rec = mysql_fetch_array($result)) {
            $arr[] = $rec["id"];
            $nbrows++;
        }
        $jsonresult = json_encode($arr);
    
    finish:
        if (!is_null($mysql)) {
            mysql_close($mysql);
        }
        if (!is_null($error)) {
            echo $error;
            echo "{'ok': false}";
        } else {
            echo "{'total': '" . $nbrows . "', 'results': '" . $jsonresult . "'}";
        }
    Im stuck on on the javascript side trying to make use of the result:

    Code:
        function onDataLoad(r, options, success) {
            if (success) {
                Ext.Msg.alert(Ext.util.JSON.decode(r));
            } else {
                Ext.Msg.alert("failure");
            }
        }
    the debugger in my browser tells me "r" is a char array (i.e. the string as it comes from php), but how can i use this now?

  2. #2
    Sencha User
    Join Date
    Jan 2011
    Posts
    47

    Default

    Firebug shows requests?
    Also ... suggest use json in errors answers.

    echo "{'total': '" . $nbrows . "', 'results': '" . $jsonresult . "'}";

    $arr_json[ "message" ] = "Error here"
    $arr_json[ "success" ] = false;
    header ( " ... json header
    echo json_encode( $arr_json


    It is more effective.

  3. #3

    Default

    sorry but that doesnt help

  4. #4
    Sencha User
    Join Date
    Dec 2010
    Location
    The Netherlands
    Posts
    70

    Default

    What is it then that you want to do with it? Ext.decode() should just decode the string (r) into an Object.

    Code:
    function onDataLoad(r, options, success) {
        if (success) {
            var resultObject = Ext.decode(r);
        } else {
            Ext.Msg.alert("failure");
        }
    }
    Try this, and see what the contents of resultObject will be.

  5. #5
    Sencha User Condor's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    24,245

    Default

    No, you want Ext.decode(r.responseText) and not Ext.decode(r).

  6. #6
    Sencha User
    Join Date
    Dec 2010
    Location
    The Netherlands
    Posts
    70

    Default

    Quote Originally Posted by Condor View Post
    No, you want Ext.decode(r.responseText) and not Ext.decode(r).
    Sorry, you're right.

Similar Threads

  1. UTF-8 in DB but accent not working with Ext.data.store
    By pp2 in forum Ext 3.x: Help & Discussion
    Replies: 3
    Last Post: 15 Jan 2011, 9:57 PM
  2. jsonreader not working for me in a Ext.data.Store
    By jonesmc123 in forum Ext 2.x: Help & Discussion
    Replies: 8
    Last Post: 6 Dec 2007, 7:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •