PDA

View Full Version : data store fed from php not working



sancho1980
18 Feb 2011, 3:45 PM
I am trying to implement a simple data store that gets its data from a php backen:


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


data.load(
{
callback: onDataLoad
}
);

and on the php side i do something as simple as:


<?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:


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?

softm
18 Feb 2011, 10:49 PM
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.

sancho1980
19 Feb 2011, 11:33 AM
sorry but that doesnt help

Ooypunk
20 Feb 2011, 3:22 AM
What is it then that you want to do with it? Ext.decode() should just decode the string (r) into an Object.


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.

Condor
21 Feb 2011, 8:12 AM
No, you want Ext.decode(r.responseText) and not Ext.decode(r).

Ooypunk
21 Feb 2011, 8:23 AM
No, you want Ext.decode(r.responseText) and not Ext.decode(r).
Sorry, you're right.