PDA

View Full Version : Ext 3.4 - Need help getting combobox to load from php script using JsonReader



compi134u
31 Aug 2011, 5:11 AM
I am just starting to play with EXTJS and am having a bit of a broblem getting a simple combobox to load with data supplied by a php script.

I have installed xampp 1.7 on default folder (xampp), extjs 3.4 on folder www. My php script works fine accessing the MySQL data and converting Json (see script below).

<?php
@ $db = mysql_pconnect('localhost','root','');
if (!$db)
{
echo 'Error: Could not open the database';
exit;
}
mysql_select_db('test');
$query = "select genre from genres";
$result = mysql_query($query);
$num_records = mysql_num_rows($result);

if ($num_records > 0)
{
while ($obj = mysql_fetch_object($result)){
$arr[]=$obj;
}
}
echo '{rows:'.json_encode($arr).'}';
// echo json_encode($arr);
mysql_close($db);
?>

The extjs code is fairly simple (see below). The form displays, but the combobox is not loading. What am I missing here. This should be simple.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Getting Started Example</title>
<link rel="stylesheet" type="text/css" href="lib/extjs/resources/css/ext-all.css" />
<script src="lib/extjs/adapter/ext/ext-base.js"></script>
<script src="lib/extjs/ext-all-debug.js"></script>
<script>
Ext.onReady(function(){
Ext.QuickTips.init();
var genres = new Ext.data.Store({
reader: new Ext.data.JsonReader({
fields:['genre'],
root:'rows'
}),
proxy: new Ext.data.HttpProxy({
url:'http://localhost/genres.php' (http://www.sencha.com/forum/'http://localhost/genres.php')
}),
autoLoad: true
});
genres.load();
var movie_form = new Ext.FormPanel ({
url: 'movie-form-submit.php',
renderTo: document.body,
frame: true,
title: 'Movie Information Form',
width: 250,
items: [{
xtype:'textfield',
fieldLabel:'Title',
name:'title'
},{
xtype:'textfield',
fieldLabel:'Director',
name:'director',
allowBlank:false
},{
xtype:'datefield',
fieldLabel:'Released',
name:'released',
disabledDays:[1,2,3,4,5]
},{
xtype:'radio',
fieldLabel:'Filmed In',
boxLabel:'Color',
name:'filmed_in'
},{
xtype:'radio',
hideLabel:false,
labelSeparator:'',
name:'filmed_in',
boxLabel:'Black & White',
},{
xtype:'checkbox',
fieldLabel:'Bad Movie',
name:'bad_movie',
},{
xtype:'combo',
name:'genreCmb',
fieldLabel:'Genre',
mode:'local',
store:genres,
displayField:'genre',
width:120
}]
});
});
</script>
</head>
<body>
<!-- Nothing in the body -->
</body>
</html>


At this point I just want to make sure my environment is set up correctly and that I can connect and load data from a database. I have tested all parts separately, but not sure what I'm missing for this simple load to work.

streha
2 Sep 2011, 8:31 AM
Hi
i am not sure but i think you have an error in your php
this

echo '{rows:'.json_encode($arr).'}';

should looks like this

echo '{"rows":'.json_encode($arr).'}';

and pls put your code in the code tag

bye