PDA

View Full Version : trouble parsing a php result from a FormPanel



kgeographer
1 Mar 2012, 5:06 PM
I've spent several hours at this, and if someone could point out my probably obvious error, much appreciated.

The following code produces a valid JSON response, according to Firebug, but the failure: alert() fires, and none of the objects, like 'response' or 'jsonObject' are created. I've studied the docs and countless examples, trying both submit and ajax methods, no luck. I successfully parse a similar jsonObject elsewhere on my site from a callback function on an HTML form; I really want to use the Ext FormPanel for this though.

This is my submit() attempt:


var fp = new Ext.FormPanel({
url: 'handlers/q_big06.php',
renderTo:'queryForm_full',
items: [
basicGroup, // set of comboboxes
durationGroup // sets of radio buttons
],
buttons: [{
text: 'Send query',
formBind: true,
handler: function() {
fp.getForm().submit({
success: function(a, b) {
var response = b.responseText;
// alert(response);
var jsonObject = eval('(' + response + ')');
// alert(jsonObject);
var cost = jsonObject.cost;
var length = jsonObject.length;
var j_format = new OpenLayers.Format.GeoJSON();
var vectorFeature = j_format.read(jsonObject.geom);
if (vectorFeature) {
// ... add it to the vectorLayer and print attribute values
vectorLayer.addFeatures(vectorFeature);
// alert('<b>Cost: </b>'+cost+'<br/><b>Length: </b>'+length+'<br/>(more or less)');
} else {
alert("Didn't get back GeoJSON it seems");
} //end if
}, // end success
failure: alert('failed'),
}); // end submit
} // end handler
},{
text: 'Reset',
handler: function(){
fp.getForm().reset();
}
}]
});

mark0978
1 Mar 2012, 8:20 PM
Shouldn't that be failure: function() {
alert('failed')
}

as it is, you are calling alert and assigning the return value to failure?

kgeographer
1 Mar 2012, 11:24 PM
thanks for noting that -- made no difference though

kg

hartste90
6 Jun 2012, 9:56 AM
I am having a similar (but opposite) issue with the form panel.

the form POSTs the information and runs login.php, but no matter what the login.php echos, the form follows the "success" code. How can I trigger the "failure" code?

the SENCHA JS:______>



Ext.define('MyApp.view.LoginPanel', {
extend: 'Ext.form.Panel',

height: 400,
width: 400,
autoScroll: true,
activeItem: 0,
layout: {
align: 'stretch',
type: 'vbox'
},
bodyPadding: 10,
title: 'LoginForm',

initComponent: function() {
var me = this;

Ext.applyIf(me, {
items: [
{
xtype: 'fieldset',
activeItem: 0,
title: 'Login',
flex: 0,
items: [
{
xtype: 'textfield',
name: 'username',
fieldLabel: 'Username',
allowBlank: false
},
{
xtype: 'textfield',
width: 255,
maintainFlex: false,
name: 'password',
fieldLabel: 'Password',
allowBlank: false
}
]
},
{
xtype: 'gridpanel',
id: 'userInfo',
title: 'User Info',
store: 'UserStore',
columnLines: false,
flex: 1,
viewConfig: {
autoRender: true,
autoShow: true
},
columns: [
{
xtype: 'gridcolumn',
dataIndex: 'Username',
text: 'Username'
},
{
xtype: 'gridcolumn',
dataIndex: 'userID',
text: 'UserID'
}
]
}
],
dockedItems: [
{
xtype: 'toolbar',
height: 27,
width: 517,
dock: 'bottom',
layout: {
pack: 'end',
type: 'hbox'
},
items: [
{
xtype: 'button',
text: 'Login',
listeners: {
click: {
fn: me.onButtonClick,
scope: me
}
}
}
]
}
]
});

me.callParent(arguments);
},

onButtonClick: function(button, e, options) {
if (this.getForm().isValid()) {
this.getForm().submit({
method: 'POST',
waitTitle: 'Connecting',
waitMsg: 'Sending data...',
url: 'login.php',
success: function(form, action) {
Ext.MessageBox.show({
title: '',
msg: 'Loading..',
progressText: 'Searching..',
width:300,
progress:true,
closable:false
});
window.location.reload();
Ext.MessageBox.show({
title:'Login Successful'
});
},
failure: function(form, action) {
Ext.Msg.alert('Login Failed!');
}
});
}
}



the LOGIN.PHP_____>

*sensitive working database info has been replaced with stubs*


<html>
<?php

//attempt to connect to the server
if (!($con = mysql_connect("testing.com","adminUser","adminPass")))
{
die('Could not connect: ' . mysql_error());
}
//select the correct database
mysql_select_db("myDatabase", $con);

//query the database
$result = mysql_query("SELECT * FROM Users WHERE userUserName='$_POST[username]' AND userPassword='$_POST[password]'", $con);

//if user and password combo doesn't exist
if(mysql_num_rows($result)==0)
{

$response = array('success' => FALSE);
echo json_encode($response);
}

else
{
//create arrays to organize results
$response = array();
$posts = array();
//for each result in the results table...
while($row = mysql_fetch_array($result))
{
$password = $row['userPassword'];
$username = $row['userUserName'];
$userID = $row['userID'];

//place the data into the associative array
$posts[] = array('username' => $username, 'password' => $password, 'ID' => $userID);
}

//put the associative arrays into their parent object 'users'
$response['users'] = $posts;

//write results to .json file
$fp = fopen('results.json', 'w');
fwrite($fp, json_encode($response));
fclose($fp);

$response = array('success' => TRUE);
echo json_encode($response);

}


?>
</html>

zboris
8 Jun 2012, 7:06 AM
Isnt failure for form load is triggerred if there are comms problem? like httcode 404, 500, no connection etc ...?

Usually - if I need to handle error result, i set some error var in response and handle it within success: listener in form ajax load.

if you need to handle it via failure listener - set your response http code to say 403 forbidden, and do your magic

makes sense?