PDA

View Full Version : Newbi: Load Data in Form



Michi_72
28 Nov 2007, 4:40 AM
Hello,

i try to load data's from a MySQL to a form, i have used tutorials, but it is not the result I want.

Here is the code of form.js:



/**
* @author Shea Frederick
*/

Ext.onReady(function(){

Ext.QuickTips.init();
Ext.form.Field.prototype.msgTarget = 'side';

var form_employee = new Ext.form.Form({
labelAlign: 'left',
labelWidth: 175,
url:'forms-submit-process.php',
buttonAlign: 'right'
});

var employee_name = new Ext.form.TextField({
fieldLabel: 'Name',
name: 'name',
width:190
});

var employee_title = new Ext.form.TextField({
fieldLabel: 'Title',
name: 'title',
width:190
});

var employee_hire_date = new Ext.form.DateField({
fieldLabel: 'Hire Date',
name: 'hire_date',
width:90,
allowBlank:false,
format:'m-d-Y'
});

var employee_active = new Ext.form.Checkbox({
fieldLabel: 'Active',
name: 'active'
});

form_employee.fieldset(
{legend:'Employee Edit'},
employee_name,
employee_title,
employee_hire_date,
employee_active
);

employee_data = new Ext.data.Store({
proxy: new Ext.data.HttpProxy({url: 'forms-submit-getdata.php?id=14'}),
reader: new Ext.data.JsonReader({},[ 'id', 'name']),
remoteSort: false
});



employee_data.on('load', function() {

employee_name.setValue(employee_data.getAt(0).data.name);

});


form_employee.addButton('Save', function(){
if (form_employee.isValid()) {
form_employee.submit({
params:{
action:'submit',
id:employee_data.getAt(0).data.id
},
waitMsg:'Saving...'
});
}else{
Ext.MessageBox.alert('Errors', 'Please fix the errors noted.');
}
}, form_employee);



form_employee.render('employee-form');


});

employee_data.load();

The script for "orms-submit-getdata.php:



<?



$mysqlhost="10.0.0.21"; // MySQL-Host angeben

$mysqluser="test"; // MySQL-User angeben

$mysqlpwd="test"; // Passwort angeben

$mysqldb="gw_management"; // Gewuenschte Datenbank angeben


$connection=mysql_connect($mysqlhost, $mysqluser, $mysqlpwd) or die

("Verbindungsversuch fehlgeschlagen");


mysql_select_db($mysqldb, $connection) or die("Konnte die Datenbank nicht

waehlen.");


$id = 14;
if ($id != '') {
$sql = "SELECT id, name FROM personal WHERE id = ".mysql_real_escape_string($id);
}

echo $sql;

$arr = array();

$rs = mysql_query($sql);

while($obj = mysql_fetch_object($rs))
{
$arr[] = $obj;
}

Echo ''.json_encode($arr).'';

?>




When i call the Script: forms-submit-getdata.php, is this the result [{"id":"14","name":"Darkpriest"}].

But unfortunately is the value not shown in the form, when i call the script. I search since four hours, but can not find an error!

Please help!

Thanks

hendricd
28 Nov 2007, 5:35 AM
Is your JSON response formatted like this:




{
success: true,
data: {
clientName: "Fred. Olsen Lines",
portOfLoading: "FXT",
portOfDischarge: "OSL"
}
}

Look at the docs. The requirements are all there.

Michi_72
28 Nov 2007, 6:43 AM
Hello,

the JSON response is now formatted like this:

{success: true, data: [{"id":"14","name":"darkpriest"}]}

Thank you for your help!

This was the error!

Thanks

hendricd
28 Nov 2007, 6:48 AM
data: should not be a array.

Michi_72
29 Nov 2007, 3:05 AM
Hello,

now data is no longer an array.

The next problem i have, when i hit "save"-Button, I want to submit my Form with all Values:
This is the script part of the save - button.



form_employee.addButton('Save', function(){
if (form_employee.isValid()) {
form_employee.submit({
params:{
action:'submit'

},
waitMsg:'Saving...'
});
}else{
Ext.MessageBox.alert('Errors', 'Please fix the errors noted.');
}
}, form_employee);


form_employee.render('employee-form');

The form calls itself, but there is nothing in the $_POST-Array!

I have no idea, what went wrong!

Please help!

Thanks

Darkpriest

tryanDLS
29 Nov 2007, 8:33 AM
What does Firebug show? Did the form fields get sent? You need to understand that what happens in PHP code is meaningless if you're not sending the right data from the client.

Michi_72
29 Nov 2007, 11:41 PM
Hello,

i have changed my script so:



form_employee.addButton('Suchen', function(){
form_employee.submit({url:'forms-submit-working.php', waitMsg:'Loading'});
});



Firebug shows under POST:

ext-comp-1005 Select a state...
hire_date
name dddd
vorname

and under Response:

{success: {msg:'Slayer'}}

The screen shows "Please wait: Loading" without end.

What is here my mistake?

Thanks

tryanDLS
30 Nov 2007, 8:23 AM
Go back and read the posts above. Your response has to have (at a minimum)

{success: true/false}
It's a boolean, not a string, not an error message.

Michi_72
30 Nov 2007, 10:05 AM
Hello,

thanks for your answers and help.

My script looks like this now:




var app= function () {
var form;
return {
init: function () {
Ext.QuickTips.init();
this.form= new Ext.form.Form ({
labelAlign: 'right',
labelWidth: 150});
this.form.fieldset ({legend: "Mitarbeiter-Daten"},
new Ext.form.TextField ({
msgTarget: 'side',
name: 'name',
fieldLabel: 'Name',
allowBlank: false,
width: 200}),
new Ext.form.TextField ({
msgTarget: 'side',
name: 'vorname',
fieldLabel: 'Vorname',
allowBlank: true,
width: 200}),
new Ext.form.TextField ({
msgTarget: 'side',
name: 'email',
fieldLabel: 'Email',
vtype: 'email',
allowBlank: true,
width: 200}));

this.form.addButton ("Suchen", function () {this.form.submit ({url: 'forms-submit-working.php'})}, this);
this.form.render ("employee-form");
this.form.load ({url: "forms-submit-getdata.php", params: { load: true }});

}
}
}();



Ext.onReady (app.init, app);



The answer from PHP looks like:

{success: true,"data":{"id":"14","name":"master","vorname":"mike","email":"[email protected]"}}

When I load the script, the form is now shown with this values. okay!

My aim is, that the form is empty when i load the site, the user fills in the name and clicks "Suchen".

This data, selected with name from mysql, will i show.

I have tried everything, but i cant solve this problems.

Is it possible different buttons for different actions (search, save and so on)....

Thanks

Darkpriest