ray73864
27 May 2007, 9:28 PM
How do i go about populating a combobox using data from a MySQL database? the code i have doesn't seem to work for some reason.
PHP Code:
<?php
$link = mysql_pconnect("localhost", "<someuser>", "<somepass>") or die("Could not connect");
mysql_select_db("chaplain") or die("Could not select database");
if (isset($_REQUEST['type'])) {
$sql = "SELECT ID, Name FROM reason WHERE Type = " . $_REQUEST['type'];
}
$arr = array();
$rs = mysql_query($sql);
while($obj = mysql_fetch_object($rs))
{
$arr[] = $obj;
}
Echo 'reasons:'.json_encode($arr).'';
?>
Dynamic Form:
/*
* Ext JS Library 1.0.1
* Copyright(c) 2006-2007, Ext JS, LLC.
* licensing@extjs.com
*
* http://www.extjs.com/license
*/
Ext.onReady(function(){
Ext.BLANK_IMAGE_URL = '/ext/resources/images/s.gif';
Ext.QuickTips.init();
// turn on validation errors beside the field globally
Ext.form.Field.prototype.msgTarget = 'side';
var ReasonDS = new Ext.data.Store({
proxy : new Ext.data.HttpProxy({url : 'searchReasons.php?type=formal'}),
reader : new Ext.data.JsonReader({root : 'reasons'},['ID','Name']),
remoteSort : false
});
var AddFormal = new Ext.form.Form({
labelAlign : 'right',
labelWidth : 175
});
AddFormal.fieldset(
{legend : 'Add Formal Record'},
new Ext.form.TextField({
fieldLabel : 'First Name',
allowBlank : false,
name : 'FirstName',
id : 'FirstName',
width : 175
}),
new Ext.form.TextField ({
fieldLabel : 'Last Name',
allowBlank : false,
name : 'LastName',
id : 'LastName',
width : 175
}),
new Ext.form.NumberField({
fieldLabel : 'Record ID',
allowBlank : true,
name : 'StudentID',
id : 'StudentID',
width : 175
}),
new Ext.form.ComboBox({
store : ReasonDS,
displayField : 'Name',
emptyText : 'Choose a reason...',
fieldLabel : 'Reason',
allowBlank : false,
name : 'Reason',
id : 'Reason',
width : 175
}),
new Ext.form.NumberField({
fieldLabel : 'Term',
allowBlank : false,
name : 'Term',
id : 'Term',
width : 175
}),
new Ext.form.DateField({
fieldLabel : 'Date Seen',
allowBlank : false,
name : 'Date',
id : 'Date',
format : 'Y/m/d',
value : '2007/05/25',
width : 175
}),
new Ext.form.TextField({
fieldLabel : 'Time Seen',
allowBlank : false,
name : 'Time',
id : 'Time',
width : 175
}),
new Ext.form.TextArea({
fieldLabel : 'Comments',
allowBlank : true,
name : 'Comments',
id : 'Comments',
width : 500
})
);
AddFormal.addButton({
text : 'Find Student',
handler : function() {
AddFormal.submit({
url : 'searchFormal.php',
waitMsg : 'Searching for student record...'
});
}
});
AddFormal.addButton({
text : 'Create Formal Record',
handler : function() {
AddFormal.submit({
url : 'addFormal.php',
waitMsg : 'Creating formal record for student...'
});
}
});
AddFormal.render('addFormal');
});
HTML File:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Capel Primary School - Chaplaincy Recording</title>
<link href="styles/intranet.css" rel="stylesheet" type="text/css" />
<!-- ExtJS files -->
<script type="text/javascript" src="/ext/adapter/yui/yui-utilities.js"></script>
<script type="text/javascript" src="/ext/adapter/yui/ext-yui-adapter.js"></script>
<script type="text/javascript" src="/ext/ext-all-debug.js"></script>
<link rel="stylesheet" type="text/css" href="/ext/resources/css/ext-all.css" />
<link rel="stylesheet" type="text/css" href="/ext/resources/css/ytheme-gray.css" />
<!-- end ExtJS files -->
<script type="text/javascript" src="js/addFormal.js"></script>
</head>
<body>
<div id="addFormal" style="background-color: LightBlue; border: 2px solid Blue; padding: 0.5em; width: 95%"></div>
</body>
</html>
PHP Code:
<?php
$link = mysql_pconnect("localhost", "<someuser>", "<somepass>") or die("Could not connect");
mysql_select_db("chaplain") or die("Could not select database");
if (isset($_REQUEST['type'])) {
$sql = "SELECT ID, Name FROM reason WHERE Type = " . $_REQUEST['type'];
}
$arr = array();
$rs = mysql_query($sql);
while($obj = mysql_fetch_object($rs))
{
$arr[] = $obj;
}
Echo 'reasons:'.json_encode($arr).'';
?>
Dynamic Form:
/*
* Ext JS Library 1.0.1
* Copyright(c) 2006-2007, Ext JS, LLC.
* licensing@extjs.com
*
* http://www.extjs.com/license
*/
Ext.onReady(function(){
Ext.BLANK_IMAGE_URL = '/ext/resources/images/s.gif';
Ext.QuickTips.init();
// turn on validation errors beside the field globally
Ext.form.Field.prototype.msgTarget = 'side';
var ReasonDS = new Ext.data.Store({
proxy : new Ext.data.HttpProxy({url : 'searchReasons.php?type=formal'}),
reader : new Ext.data.JsonReader({root : 'reasons'},['ID','Name']),
remoteSort : false
});
var AddFormal = new Ext.form.Form({
labelAlign : 'right',
labelWidth : 175
});
AddFormal.fieldset(
{legend : 'Add Formal Record'},
new Ext.form.TextField({
fieldLabel : 'First Name',
allowBlank : false,
name : 'FirstName',
id : 'FirstName',
width : 175
}),
new Ext.form.TextField ({
fieldLabel : 'Last Name',
allowBlank : false,
name : 'LastName',
id : 'LastName',
width : 175
}),
new Ext.form.NumberField({
fieldLabel : 'Record ID',
allowBlank : true,
name : 'StudentID',
id : 'StudentID',
width : 175
}),
new Ext.form.ComboBox({
store : ReasonDS,
displayField : 'Name',
emptyText : 'Choose a reason...',
fieldLabel : 'Reason',
allowBlank : false,
name : 'Reason',
id : 'Reason',
width : 175
}),
new Ext.form.NumberField({
fieldLabel : 'Term',
allowBlank : false,
name : 'Term',
id : 'Term',
width : 175
}),
new Ext.form.DateField({
fieldLabel : 'Date Seen',
allowBlank : false,
name : 'Date',
id : 'Date',
format : 'Y/m/d',
value : '2007/05/25',
width : 175
}),
new Ext.form.TextField({
fieldLabel : 'Time Seen',
allowBlank : false,
name : 'Time',
id : 'Time',
width : 175
}),
new Ext.form.TextArea({
fieldLabel : 'Comments',
allowBlank : true,
name : 'Comments',
id : 'Comments',
width : 500
})
);
AddFormal.addButton({
text : 'Find Student',
handler : function() {
AddFormal.submit({
url : 'searchFormal.php',
waitMsg : 'Searching for student record...'
});
}
});
AddFormal.addButton({
text : 'Create Formal Record',
handler : function() {
AddFormal.submit({
url : 'addFormal.php',
waitMsg : 'Creating formal record for student...'
});
}
});
AddFormal.render('addFormal');
});
HTML File:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Capel Primary School - Chaplaincy Recording</title>
<link href="styles/intranet.css" rel="stylesheet" type="text/css" />
<!-- ExtJS files -->
<script type="text/javascript" src="/ext/adapter/yui/yui-utilities.js"></script>
<script type="text/javascript" src="/ext/adapter/yui/ext-yui-adapter.js"></script>
<script type="text/javascript" src="/ext/ext-all-debug.js"></script>
<link rel="stylesheet" type="text/css" href="/ext/resources/css/ext-all.css" />
<link rel="stylesheet" type="text/css" href="/ext/resources/css/ytheme-gray.css" />
<!-- end ExtJS files -->
<script type="text/javascript" src="js/addFormal.js"></script>
</head>
<body>
<div id="addFormal" style="background-color: LightBlue; border: 2px solid Blue; padding: 0.5em; width: 95%"></div>
</body>
</html>