0

I have a combobox and i want to get data from php server
Here is my Index.html code http://jsfiddle.net/UQcgA/

Here is my data.php file.
But when i click to combo then nothing show :(. How can i fix that thanks

<?php
// function to create json
function GetCategories() {
    $categories = "{'rows':[";
    $categories = "{'id':'1', 'name':'Category 1'},";
    $categories .= "{'id':'2', 'name':'Category 2'},";
    $categories .= "{'id':'3', 'name':'Category 3'}";
    $categories .= "]}";
    return $categories;
}
echo GetCategories(); // sent to client
?>
DeLe
  • 2,442
  • 19
  • 88
  • 133
  • What does the returned JSON string look like? – existdissolve Jul 08 '13 at 03:11
  • @existdissolve it look {'id':'1', 'name':'Category 1'},{'id':'2', 'name':'Category 2'},{'id':'3', 'name':'Category 3'}]} – DeLe Jul 08 '13 at 03:20
  • it needs to look like: {'rows':[{'id':'1', 'name':'Category 1'},{'id':'2', 'name':'Category 2'},{'id':'3', 'name':'Category 3'}]}. You are missing a concatenation in your code. But Musa suggested, you really shouldn't build JSON by hand, if you can help it. – existdissolve Jul 08 '13 at 12:57
  • @existdissolve I using echo "{'rows':[{'id':'1', 'name':'Category 1'},{'id':'2', 'name':'Category 2'},{'id':'3', 'name':'Category 3'}]}"; But it's not working :( – DeLe Jul 08 '13 at 16:38
  • What is returned when you view the request in developer tools or Fire bug? – existdissolve Jul 08 '13 at 17:41

1 Answers1

0

The main problem is with your store definition. You are using the definition for ExtJS 3. This is a correct way to define store in ExtJS 4.1:

        var values = new Ext.data.Store({
            fields: [
                {name: 'id', type: 'integer'},
                {name: 'name', type: 'string'}
            ],
            proxy: new Ext.data.HttpProxy({
                url: 'data.php',
                reader: {
                    type: 'json',
                    root: 'rows'
                }
            }),
            autoLoad: true
        });

You have a problem with your json data too (you're missing a period in the 2nd line also). This will work:

// function to create json
function GetCategories() {
    $categories = '{"rows": [';
    $categories .= '{"id": "1", "name": "Category 1"},';
    $categories .= '{"id": "2", "name": "Category 2"},';
    $categories .= '{"id": "3", "name": "Category 3"}';
    $categories .= "]}";
    return $categories;
}
echo GetCategories(); // sent to client
?>

NOTE: it is always a best practice to build the JSON string using json_encode see this answer.

Community
  • 1
  • 1
vahissan
  • 2,322
  • 16
  • 26