PDA

View Full Version : Submit to PHP



ConnorStr
20 Jun 2009, 2:31 AM
I am having issues submitting a form created with ext, to a php file which should then return success/fail and report this to the js form.

submit button on form.js


buttons:[{
text:'Login',
formBind: true,
// Function that fires when user clicks the button
handler:function(){
login.getForm().submit({
method:'POST',
waitTitle:'Connecting',
waitMsg:'Sending data...',

success:function(){
Ext.Msg.alert('Status', 'Success Login..!', function(btn, text){
if (btn == 'ok'){
var redirect = './';
window.location = redirect;
}
});
},
failure:function(form, action){
if(action.failureType == 'server'){
obj = Ext.util.JSON.decode(action.response.responseText);
Ext.Msg.alert('Login Failed!', obj.errors.reason);
}else{
Ext.Msg.alert('Warning!', 'Authentication server is unreachable : ' + action.response.responseText + \"abcd\");
}
login.getForm().reset();
}
});
}
}
form.php


<?PHP
include($_SERVER['DOCUMENT_ROOT'].'/common.php');
$loginUsername = $_POST["loginUsername"];
$loginPassword = $_POST["loginPassword"];
$fi=$loginUsername[0]; // Selecting the first letter of the username for $fi.
$fi=ucwords($fi); // CAPSING the $fi.
$ln=substr($loginUsername, 1); // Selecting the last name out of the username.
$ln=ucwords(strtolower($ln)); // CAPSING the first letter of the last name.
//echo "{success: false, errors: { reason: 'Login failed. Try again. Ref: Pass.' }}";

if($db->query("SELECT f_name, l_name, pass FROM users WHERE l_name='".$ln."' and password='".md5($loginPassword)."'")){
//Set the sessions/cookies here.
echo "{success: false}";
}else{
echo "{success: false, errors: { reason: 'Login failed. Try again. Ref: First Init.' }}";
}
?>


No matter what is inputted in the form, it always succeeds, and thats not how it should work being a login form..

AWCI
20 Jun 2009, 5:02 AM
try this syntax for "echo success"



echo '{"success":true}';
}else{
echo '{"success":false}';
}

ConnorStr
20 Jun 2009, 9:54 AM
That didn't work, it still only passes it as success.

ostghost
20 Jun 2009, 1:11 PM
try to locate problem

itīs on server or client side?

1. could you see server response? instal firebug as bwroser plugin for ria applications for example based extjs itīs helpful you could se your ajax calls live

http://getfirebug.com/

2. if firebug installed whatīs server json response false or true?

3. better way for hnadling json in php than imploding part of strings is json_encode via http://us3.php.net/manual/en/function.json-encode.php

for example your response should be build like this



<?php
$success = false;
if($db->query('SELECT *....')) {

// if if :-) is tru set varaibel succcess to true
// if if is false variable reamins false
$success = true;

}

$arr = array ('success'=> true);
echo json_encode($arr);
?>

if you are using some library like zend frameqork it could have own json handling function but php standard json_encode and json_decode are quite fine.

problem with true and false could be in way how are u putting json string. it= s sounds strange but few months ago before i start to use zend_json i fall in problem that extsj don= t want to understand true as string or wrong way of interpeting number columns even if they werere sending as {id:1} not {id:'1'}

so try tu use json_encode instead of string

ConnorStr
21 Jun 2009, 12:32 PM
Im not quite sure how to use firebug, and Im still having problems after I tried the above stated way, with json_encode...

ConnorStr
21 Jun 2009, 1:03 PM
This is what my submitting page looks like now.



$loginUsername = $_POST["loginUsername"];
$loginPassword = $_POST["loginPassword"];
$fi=$loginUsername[0]; // Selecting the first letter of the username for $fi.
$fi=ucwords($fi); // CAPSING the $fi.
$ln=substr($loginUsername, 1); // Selecting the last name out of the username.
$ln=ucwords(strtolower($ln)); // CAPSING the first letter of the last name.
//echo "{success: false, errors: { reason: 'Login failed. Try again. Ref: Pass.' }}";
$query=$db->query("SELECT id, f_name, l_name, password FROM users WHERE l_name='".$loginUsername."' AND password='".md5($loginPassword)."'")
$success = false;
if($query){
$uia=array($query[0],$loginUsername,$loginPassword);
$_SESSION['__SSADMIN']=true;
$_SESSION['__UINFO']=implode("||",$uia);
$success = true;
}
$arr = array ('success'=> $success);
echo json_encode($arr);

and yes I know I am using just the username as the last name, right now.

aot2002
8 Jul 2010, 5:39 PM
It's because you need to return failure json not success

$o = array ('failure'=> true);
$tmp = json_encode($o);