View Full Version : Flash/PHP/MySQL Login, have to click twice! (code posted and example on web server)

12-28-2005, 10:12 PM
I have been having this problem for a few days now... Any help would be appreciated. If you see a better way to do things please feel free to voice it. :)

Example of problem here: (login and registration works)

Flash 8 Source:
Flash 7 Source:

The Problem:
After entering in the login information or when calling data from the php, you have to click twice for Flash to recognize that the information has been called. What can I do so you don't have to click twice?

The Flash Code
on (press)
//login_state 0 = No Errors Found
//login_state 1 = Error Found, Bad Password.
//login_state 2 = Error Found, Bad Account Name.
//login_state_num = 5 = Too many attempts
login_state = 0
//login_state_num++ 'Disabled for now

if (account_password_input.text == "")
error_php_update = "Please Enter Your Password.";
login_state = 1;
if (account_name_input.text == "")
error_php_update = "Please Enter Your Account Name.";
login_state = 2;
//No errors within flash user entry, proceed to process data and pass to PHP
if (login_state == 0)
error_php_update = "Flash - Beginning Login Process";
account_name = account_name_input.text;
account_password = account_password_input.text;
//Passes the account_name and account_password to the Login.php script.
loadVariablesNum ("php/login.php?sql_account_name="+account_name+"&sql_account_password="+account_password+"&R="+random(999), 5);
/*Disabled for now if (login_state_num == 5)
error_php_update = "Login Failed. Too many attempts.";
//Loginstate_php_update comes from the PHP File. This drops the move back to the Main SWF (for shared Libary resources) and moves onto the next movie. (MainMenu.swf)
if (loginstate_php_update == 1)

The PHP Code
/* This login script accesses the database and checks to see whether the Users Login (sql_account_name) exists.
If you have a registered user name, the data will retrieved from the database and you will proceed to the game. */

// Sets the variables needed to connect to the database.
$DBhost = "HOST";
$DBuser = "USER";
$DBpass = "PASS";
$DBName = "DBNAME";
$DBTable = "TABLE";

/* This line of Code changes the name to all UPPERCASE. This is so that the login Name is not case sensitive. */
$sql_account_name = strtoupper ($sql_account_name);

/* This line takes out everything from the $sql_account_name variable that is not a capital or lowercase letter or a integer
between 0 and 9. This line is only for security purposes so that users can not enter anything that could disrupt the database.*/
$sql_account_name = ereg_replace("[^A-Za-z0-9 ]", "", $sql_account_name);

/* Connection to the database starts here <----------->*/

/* Checks to see if you are able to connect to the MySQL Server and database*/
$DBconn = mysql_connect($DBhost,$DBuser,$DBpass);
if (!$DBconn)
print "error_php_update=DB_Error 0001: Cannot connect to the $DBhost Server.";
if (! @mysql_select_db("$DBName") )
print "error_php_update=DB_Error 0002: Cannot connect to the $DBname database.";

//Checks Preforms the query and errors if it fails.
$query = "SELECT * FROM township_account_data WHERE account_name = '$sql_account_name'";
$result = mysql_query($query);
if (!$result)
print "error_php_update=DB_Error 0003: Cannot preform query.";

While ($row = mysql_fetch_array($result))

$account_name = $row['account_name'];
$account_password = $row['account_password'];

/* Compares the Username and Password if successful, continue. If not Name not in Database.*/
if ($sql_account_name == $account_name & $sql_account_password == $account_password) {
print "error_php_update=Success Login Complete.&loginstate_php_update=1";
else {
print "error_php_update=DB_Error 0004 - Username and password not found - Please verify the information.";


Flash Gordon
12-28-2005, 10:16 PM
that coding is too sloppy to read. Post your fla and PHP file for better answers.

Also, not good practice to do on(press) mc.onPress = function() is much better.

12-28-2005, 10:34 PM
Source of the "login" has been posted. I will look into mc.onPress = function()

Flash Gordon
12-28-2005, 10:35 PM
most people don't have flash 8 yet, wanna zip it up at 7?

12-28-2005, 10:39 PM
There we go. Link under the first source.

Flash Gordon
12-28-2005, 10:42 PM
loadVariablesNum("php/login.php?sql_account_name="+account_name+"&sql_account_password="+account_password+"&R="+random(999), 5);Never a good idea for loadVaribles.

LoadVars is much better. Especially with the onLoad() function. Probably the data is not fully load on the first click. The second click probably is just a quincidence.

12-28-2005, 11:12 PM
Thank you, I am finding some information on the net about loadVars().

Do you have to have any good resources off hand that you know about? If not, no worries, I have enough to get me in the right direction.

Flash Gordon
12-28-2005, 11:16 PM
well searching the server side forums here for examples is a good start.


12-29-2005, 04:34 PM
This really helped get me started in the right direction.