sintax.era
26 Feb 2008, 7:40 PM
Im gonna post this snippet, because I spent about nine thousand hours in these forums trying to figure out how to do this. And in the end I went and read the source for json_encode, and the specification for JSON itself... and figured it out. lol.
<?php
/* ... here is the part of the code that accesses the mySQL database and inserts the form data... superfluous to this excercise, just know that $sql holds the final query string, and $link is the mySQL persistent connection */
$a = array(); //main array to encode
$e = array(); //array to hold list of validation errors
$a['success'] = true; // Lets assume this will work, then set out to prove otherwise
{ //this chunk just forces some of the fields to fail validation, you would actually want to check the values and decide whether they pass or not :)
$e[] = array('id'=>'terms','msg'=>'lol'); //terms is the 'name' config var of a combobox
$e[] = array('id'=>'company','msg'=>'rofl');
$e[] = array('id'=>'date','msg'=>'rofl');
$a['success'] = false; // Dont want our application to think it worked while having errors!
}
if ($a['success']) {
$a['sql'] = $sql;
mysql_query($sql,$link) or $a['success'] = false; //try to submit the form via mysql
$a['errormessage'] = mysql_error($link); //if theres an error, put it in the error message field
$a['successmessage'] = 'Quote Added.';
$a['id'] = mysql_insert_id(); //set the 'id' field to the number of the new database entry
}
$a['errors'] = $e; //add the error array to our return
print json_encode($a); //encode in JSON and return to our ext2 application
?>
There are probably a lot of missed checks etc there, but I hope you get the idea.
Heres the bit from the application that receives the result.
buttons: [
{
text: 'Save',
type: 'submit',
handler: function() {
quoteDialogGUI.form.submit({ //quoteDialogGUI is the formPanel
method: 'POST',
url: 'php/addquote.php', //the php script above
success: function(f, a) { //f is the form, a.result is the JSON our PHP sent
f.setValues(a.result.data);
Ext.Msg.alert('Success','Quote #'+a.result.id+' added');
},
failure: function(f, a) {
f.markInvalid(a.result.errors);
}
})
}
},{
text: 'Cancel'
}]
Here is the lazy config for the 'terms' combobox...
{ xtype:"combo",
fieldLabel:"Terms",
name:"terms",
hiddenName:"terms",
store:dsTerms,
valueField:"id",
displayField:"title",
mode:"local"
}
Hopefully I have helped someone with something :)
Any questions, please ask
-Tony
<?php
/* ... here is the part of the code that accesses the mySQL database and inserts the form data... superfluous to this excercise, just know that $sql holds the final query string, and $link is the mySQL persistent connection */
$a = array(); //main array to encode
$e = array(); //array to hold list of validation errors
$a['success'] = true; // Lets assume this will work, then set out to prove otherwise
{ //this chunk just forces some of the fields to fail validation, you would actually want to check the values and decide whether they pass or not :)
$e[] = array('id'=>'terms','msg'=>'lol'); //terms is the 'name' config var of a combobox
$e[] = array('id'=>'company','msg'=>'rofl');
$e[] = array('id'=>'date','msg'=>'rofl');
$a['success'] = false; // Dont want our application to think it worked while having errors!
}
if ($a['success']) {
$a['sql'] = $sql;
mysql_query($sql,$link) or $a['success'] = false; //try to submit the form via mysql
$a['errormessage'] = mysql_error($link); //if theres an error, put it in the error message field
$a['successmessage'] = 'Quote Added.';
$a['id'] = mysql_insert_id(); //set the 'id' field to the number of the new database entry
}
$a['errors'] = $e; //add the error array to our return
print json_encode($a); //encode in JSON and return to our ext2 application
?>
There are probably a lot of missed checks etc there, but I hope you get the idea.
Heres the bit from the application that receives the result.
buttons: [
{
text: 'Save',
type: 'submit',
handler: function() {
quoteDialogGUI.form.submit({ //quoteDialogGUI is the formPanel
method: 'POST',
url: 'php/addquote.php', //the php script above
success: function(f, a) { //f is the form, a.result is the JSON our PHP sent
f.setValues(a.result.data);
Ext.Msg.alert('Success','Quote #'+a.result.id+' added');
},
failure: function(f, a) {
f.markInvalid(a.result.errors);
}
})
}
},{
text: 'Cancel'
}]
Here is the lazy config for the 'terms' combobox...
{ xtype:"combo",
fieldLabel:"Terms",
name:"terms",
hiddenName:"terms",
store:dsTerms,
valueField:"id",
displayField:"title",
mode:"local"
}
Hopefully I have helped someone with something :)
Any questions, please ask
-Tony