Advertisement
  1. Code
  2. JavaScript

Create a Flash Login System Using PHP and MySQL – Part 2

Scroll to top
8 min read

In Part 1 we created the front end login system for users to enter their details. In this tutorial we will create a registry form that will allow users to register. This will then add their details to the database, after which they will be able to login.


Step 1: Setup Document Class

Firstly, let's setup our document class and Flash file. Open a new Flash file, then call it register.fla. Next, create an actionscript file and call it register.as

Then type register in the class text field in the Properties panel.


Step 2: Setup Code Structure

Now we have connected our document to our code, we need to create our class in the as file.

1
2
package {
3
4
	import flash.display.*;
5
6
	public class register extends MovieClip {
7
	
8
		public function register ():void {
9
		
10
			trace("success");
11
		
12
		}
13
	
14
	}
15
16
}

Step 3: Create The Registry Form Interface

Draw a 460x300px filled rectangle and choose what background color you like. Create the title - Register With Us - and a text field - Register.

Your interface should look like this:


Step 4: Setup The Form Fields

Create three input text fields with the instance names of username_text, password_text and userbio_text.


Step 5: Create The Register Button

Click the text field with "Register" that you created in Step 3 and convert it to a movie clip. Give it an instance name of register_button.

By now your interface should look like this:


Step 6: Create The Result Text

Create a new dynamic textfield next to the register and give it an instance name of result_text. We have completed all the objects we need. Our finished interface should look like this.


Step 7: Get Assets From Part 1

To save you time I will not cover the connection in this Part as you can learn that in Part 1. Get the connect.php from the php folder in Part 1. I have also included it in the Part 2 Source zip download.

That's all we need from Part 1. Now let's create the actionscript code.


Step 8: Create Object Settings - Register Button

First we need to give the register button a rollover cursor. We do this by using the buttonMode property.

1
2
register_button.buttonMode = true;

That is all we need to do to format our register button.


Step 9: Setting up The Register Button

We now need our register button to run a function that validates the input of a user. We can do this by adding an event listener and triggering the function when the user presses the button.

1
2
/*

3
when register button is pressed then the checkForm function runs.

4
*/
5
6
register_button.addEventListener(MouseEvent.MOUSE_DOWN, checkForm);

Step 10: Validate the Form

Before the user can submit the form they have to fill in all the fields. Remember to import the flash.events.* classes into your AS file. Follow the comments...

1
2
public function checkForm (e:MouseEvent):void {
3
4
	/*

5
	If all of the fields have more than one character sendForm() inits. 

6
	If not then the result_text will tell them to fill them all in.

7
	*/
8
9
	if (username_text.text != "" && password_text.text != "" && userbio_text.text != "") {
10
			
11
		sendForm();
12
			
13
   	} else {
14
			
15
		result_text.text = "PLEASE FILL IN ALL FIELDS";
16
			
17
	}
18
		
19
}

Step 10: Set up the URLVariables Class

Remember to import the flash.net.* classes into your AS file. The URLVariables class is used to store data in that you would send off to a external source at a later date.

We need to create a variable to hold this data in:

var phpVars:URLVariables = new URLVariables();

We use instance variables to store data:

1
2
phpVars.username = username_text.text;
3
phpVars.password = password_text.text;
4
phpVars.userbio = userbio_text.text;

Step 11: Set up the URLRequest Class

Next we need to make a URL request to get the location of our php file. This URLRequest is also what we pass our php variables through. When we load this request the php variables are sent to the php file.

1
2
var urlRequest:URLRequest = new URLRequest("php/register.php");
3
4
/*

5
the POST method is used here so we can use php's $_POST function in order to receive our php variables.

6
*/
7
8
urlRequest.method = URLRequestMethod.POST;
9
10
/*

11
this attaches our php variables to the url request

12
*/
13
14
urlRequest.data = phpVars;

Step 12: Set up the URLLoader Class

The URLLoader Class allows you send & receive data from external sources. In this case we will be sending our URLVariables data to the php file via our URLRequest. Follow the comments...

1
2
/*

3
we use the URLLoader class to send the request URLVariables to the php file

4
*/
5
			
6
var urlLoader:URLLoader = new URLLoader();
7
urlLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
8
			
9
/*

10
runs the function once the php file has spoken to Flash

11
*/
12
			
13
urlLoader.addEventListener(Event.COMPLETE, showResult);
14
15
/*

16
we send the request to the php file

17
*/
18
			
19
urlLoader.load(urlRequest);

Step 13: Create the showResult() Function

In the sendForm() function we added a listener to run the showResult() function when the php file had been executed. Now let's create it...

1
2
/*

3
function to show result

4
*/
5
		
6
public function showResult (e:Event):void {
7
8
	// e.target.data is referring to the php file's output. We define result_message in the php file later on.

9
		
10
	result_text.text = "" + e.target.data.result_message;
11
		
12
}

Step 14: Create the PHP File

Create a new php file, name it register.php and save it in a new folder called php.


Step 15: Flash to PHP Communication

In order to continue we need to be sure whether our php and Flash files are talking to each other. Open your text editor of choice and open the register.php you just created. Again, follow the comments...

1
2
3
<?php 
4
5
/*

6
use connect.php to connect out database

7
*/
8
9
include_once("connect.php");
10
11
/*

12
create the test POST VAR - $_POST['username'] is used to phpVars.username from the phpVars variable we set.

13
*/
14
15
$username = $_POST['username'];
16
17
/*

18
print the output - This is sent back to flash. As you can see result_message has now been defined.

19
*/
20
21
print "result_message=$username";
22
23
?>

Now upload the files to your server for testing and check it out. If the resultant text is now the same as the username you have successful communication between Flash and php. Now delete the contents of register.php.


Step 16: Include Connection File

Now we have successful connection we can connect to our database from Part 1. We do this by including our connect.php file we made in Part 1.

1
2
3
<?php 
4
5
include "connect.php";
6
7
?>

Step 17: Create POST Vars

We need to create POST vars to receive the php vars we sent and defined as POST variables.

1
2
/*

3
create POST vars to receive data from flash

4
*/
5
6
$username = $_POST['username'];
7
$password = $_POST['password'];
8
$userbio = $_POST['userbio'];
9
<?php 
10
11
/*

12
connect to database

13
*/
14
15
include "connect.php";
16
17
/*

18
create POST vars to receive data from flash

19
*/
20
21
$username = $_POST['username'];
22
$password = $_POST['password'];
23
$userbio = $_POST['userbio'];
24
25
?>

Step 18: Create the SQL Statement

Let's now create the SQL statement that will add our values and store them in a new record in the database. Follow the comments..

1
2
3
/*

4
INSERT INTO users - This means we want to insert a record into the users table.

5


6
(username, password, user_bio) - We then specify the fields we want to add our data to.

7


8
VALUES ('$username', '$password', '$userbio') - We then specify which data we want in each field.

9


10
*/
11
12
$sql = "INSERT INTO users (username, password, user_bio) VALUES ('$username', '$password', '$userbio')";

Step 19: Back to Flash

1
2
3
/*

4
Next we send off the query to communicate with the database. To send the result_message back to flash we use the exit function. 

5
*/
6
7
mysql_query($sql) or exit("result_message=Error");
8
9
exit("result_message=Success");

Step 20: Completed PHP Script

1
2
3
<?php 
4
5
/*

6
use connect.php to connect out database

7
*/
8
9
include_once("connect.php");
10
11
/*

12
create POST vars to receive data from flash

13
*/
14
15
$username = $_POST['username'];
16
$password = $_POST['password'];
17
$userbio = $_POST['userbio'];
18
19
/*

20
create the mysql query

21
*/
22
23
$sql = "INSERT INTO users (username, password, user_bio) VALUES ('$username', '$password', '$userbio')";
24
25
mysql_query($sql) or exit("result_message=Error");
26
27
exit("result_message=Success");				  
28
29
?>

Step 21: Completed ActionScript File

1
2
package {
3
	
4
	import flash.display.*; 
5
	import flash.events.*;
6
	import flash.net.*;
7
	
8
	public class register extends MovieClip {
9
	
10
		public function register ():void {
11
			
12
			/*

13
			First we need to give the register button a rollover cursor. We do this by using the buttonMode property.

14
			*/
15
		
16
			register_button.buttonMode = true;
17
			
18
			/*

19
			give the register button a mouse event

20
			*/
21
			
22
			register_button.addEventListener(MouseEvent.MOUSE_DOWN, checkForm);
23
			
24
			/*

25
			set all the fields to no characters

26
			*/
27
			
28
			username_text.text = "";
29
			password_text.text = "";
30
			userbio_text.text = "";
31
		
32
		}
33
		
34
		/*

35
		validate the user's input

36
		*/
37
		
38
		public function checkForm (e:MouseEvent):void {
39
		
40
			if (username_text.text != "" && password_text.text != "" && userbio_text.text != "") {
41
			
42
				sendForm();
43
			
44
			} else {
45
			
46
				result_text.text = "PLEASE FILL IN ALL FIELDS";
47
			
48
			}
49
		
50
		}
51
		
52
		/*

53
		function we use to send the form

54
		*/
55
		
56
		public function sendForm ():void {
57
			
58
			/*

59
			we use the URLVariables class to store our php variables 

60
			*/
61
			
62
			var phpVars:URLVariables = new URLVariables();
63
			
64
			phpVars.username = username_text.text;
65
			phpVars.password = password_text.text;
66
			phpVars.userbio = userbio_text.text;
67
	
68
			/*

69
			we use the URLRequest method to get the address of our php file and attach the php vars.

70
			*/
71
			
72
			var urlRequest:URLRequest = new URLRequest("php/register.php");
73
			
74
			/*

75
			the POST method is used here so we can use php's $_POST function in order to recieve our php variables.

76
			*/
77
			
78
			urlRequest.method = URLRequestMethod.POST;
79
			
80
			/*

81
			this attaches our php variables to the url request

82
			*/
83
			
84
			urlRequest.data = phpVars;		
85
			
86
			/*

87
			we use the URLLoader class to send the request URLVariables to the php file

88
			*/
89
			
90
			var urlLoader:URLLoader = new URLLoader();
91
			urlLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
92
			
93
			/*

94
			runs the function once the php file has spoken to flash

95
			*/
96
			
97
			urlLoader.addEventListener(Event.COMPLETE, showResult);
98
99
			/*

100
			we send the request to the php file

101
			*/
102
			
103
			urlLoader.load(urlRequest);
104
			
105
		}
106
		
107
		/*

108
		function to show result

109
		*/
110
		
111
		public function showResult (e:Event):void {
112
		
113
			result_text.text = "" + e.target.data.result_message;
114
		
115
		}
116
	
117
	}
118
119
}

Conclusion

Now you can go to the demo, register, then go to the login page and try it out. Thanks for reading this two-part series!

Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.