I’m creating a login in Flash that connects to a MySql database through PHP.
I’m using sendAndLoad() and the variables are being send to PHP without any prolems, however it does not seem to be returning any info resulting in a “invalid username/pass” message.
I’m fairly sure the problem lies in the PHP but not too sure where! Do I have to declare the result_lv variable in the PHP somewhere? How and where?
Any suggestions gratefully appreciated!
PDALOGIN.PHP
<?
$username=$_GET['username'];
$password=$_GET['password'];
//connect to database
//if($username && $password){
//changed to just username to simplify for testing!
if ($username){
mysql_pconnect("host", "name", "pass") or die ("didn't connect to mysql");
mysql_select_db("artefac_database") or die ("no database");
//make query
$query = "SELECT * FROM 'users' WHERE username = '$username' ";
$result = mysql_query( $query ) or die ("didn't query");
//if EXACT match
$num = mysql_num_rows( $result );
if ($num == 1){
echo "sucess";
} else {
echo "no match ";
}
}
?>
AS on Frame 1 -
// classes.
import mx.controls.*;
// strict type instances on the Stage.
var status_lbl:Label;
var username_lbl:Label;
var username_ti:TextInput;
var password_lbl:Label;
var password_ti:TextInput;
// change the colors of the labels
this.username_lbl.setStyle("color", 0x999999);
this.password_lbl.setStyle("color", 0x999999);
/* Define a click handler for the submit_btn Button instance,
which calls the checkForm function (defined below). */
submit_btn.clickHandler = function() {
checkForm();
};
/* Create a listener object used with both the
username_ti and password_ti TextInput instances -
"listens" for the "enter" event,
which triggers when the enter key is pressed. */
var formListener:Object = new Object();
formListener.enter = function(evt) {
checkForm();
};
username_ti.addEventListener("enter", formListener);
password_ti.addEventListener("enter", formListener);
// set the form focus to the username_ti TextInput instance.
Selection.setFocus(username_ti);
/* define the checkForm function and use LoadVars to send login information to the server to validate. */
function checkForm() {
// ensure that the username_ti field isn't blank.
if (username_ti.text.length == 0) {
// if the username_ti field is empty, display an error message
status_lbl.text = "<font color=\"#FF0000\">Please enter user name.</font>";
// set the form focus to the username_ti TextInput instance.
Selection.setFocus(username_ti);
// exit the checkForm function.
return false;
}
/* if the password_ti TextInput instance is blank,
display an error message and exit the checkForm function. */
if (password_ti.text.length == 0) {
status_lbl.text = "<font color=\"#FF0000\">Please enter password.</font>";
Selection.setFocus(password_ti);
return false;
}
// clear the status_lbl Label instance
status_lbl.text = "";
/* define two instances of the LoadVars object.
One is used to hold the variables being sent to the server-side script,
and the other one is used to hold the variables returned by the
LoadVars.sendAndLoad method. */
var result_lv:LoadVars = new LoadVars();
var login_lv:LoadVars = new LoadVars();
// copy the value of the two TextInput components into the login_lv LoadVars instance.
login_lv.username = username_ti.text;
login_lv.password = password_ti.text;
/* send the variables in the login_lv instance to the server-side script
using the POST method (send as Form variables rather than along the URL) and place the results returned in the result_lv instance. */
login_lv.sendAndLoad("http://www.artefactproject.com/pdalogin.php", result_lv, "POST");
// When the results are received from the server...
result_lv.onLoad = function(success:Boolean) {
// If Flash is able to successfully send and load the variables from the server-side script...
if (success) {
// if the server returned the value of isValidLogin with a value of 1...
if (this.isValidLogin == 1) {
status_lbl.text = "<font color=\"#009900\"><b>login successful.</b></font>";
// else the login information wasn't valid
} else {
status_lbl.text = "<font color=\"#FF0000\">invalid user name / password.</font>";
// set the form focus to the username_ti TextInput instance and select the existing text.
Selection.setFocus(username_ti);
Selection.setSelection(0, username_ti.text.length);
}
/* this code is only executed if for some reason the SWF is
unable to connect to the remote page defined in LoadVars.sendAndLoad */
} else {
status_lbl.text = "<b>Unable to connect to login URL</b>";
username_ti.enabled = false;
password_ti.enabled = false;
submit_btn.enabled = false;
}
};
return true;
}