Connecting a Checkbox to a php file

The Flash file

This is a basic PHP tutorial how to use the flash Loadvars object, send and receive data. In addition I have added a checkbox to show how to handle data created by a simple component. Let's start with the flash file. We need two textfields: input and dynamic, a checkbox and a button. We give all those objects names. Further we need to create two Loadvars objects: to send out the data to the server and to receive the data from the php file.


stop ();
		//
// importing textinput, button and checkbox classes
var myName:mx.controls.TextInput;
var signup:mx.controls.CheckBox;
var submitBut:mx.controls.Button;
var messageBox:TextField;
		//
// LoadVars objects
var lv:LoadVars;
lv = new LoadVars ();
var lvAnswer:LoadVars;
lvAnswer = new LoadVars ();
		//
// button label
submitBut.label = "Submit";
		

Next we work with the checkbox. We create a var myCheckbox and give it a default value in case the user does not touch the checkbox at all. Then we create a listener for the checkbox in case the user selcts the checkbox.


		//
// we give "myCheckbox" a default value
var myCheckbox:String;
myCheckbox = "not selected";
		//
// we create a listener for the checkbox
var checkListener:Object = new Object ();
checkListener.click = function () {
	//
	// here we let the user select
	if (signup.selected) {
	       myCheckbox = "isSelected";
	} else {
	       myCheckbox = "not selected";
	}
};
signup.addEventListener ("click", checkListener);
		

Finally we send out all the data. All variables which we want to send out are sent via the loadvars object lv. if we forget to add that we will not send that particular var. That is the big advantage of using loadvars instead of the former loadvariablesNum. The sendAndload method has 3 parameters: the url for the php file, the targetobject, which is another Loadvars and a method used to send the data. Then finally we use the onload method to retrieve the data from the php file. "result" is a var from the php file. It is important here to refer to the Loadvars object lvAnswer by using "this".


		//
// sending out the data
submitBut.onRelease = function () {
	messageBox.text="Please wait.";
	lv.myName = myName.text;
	lv.myCheckbox = myCheckbox;
	lv.action = "send";
	lv.sendAndLoad ("form.php", lvAnswer, "POST");
	//
	// receiving the data from the php file
	lvAnswer.onLoad = function () {
	       messageBox.text = this.result;
	};
};
		

The PHP file

Next we need to create a PHP file, which is shown below. First we test if the var myName from the flash file has a value. If not we skip the rest and give an error message. We could have put this trap in the flash file but I have added it here to show another possibility. Usually I create two traps. Next we use a switch function with the parameter action from the flash file. If the value is "send" we proceed to the function, which holds the two var from our flash file. Now we create a success function with the final printout which we want. Since this is a string it has to be in quotations. Unlike in flash in PHP variables with the prefix $ are still recognized as variables within quotations. Then finally we send out the success message. The variable result will have a & prefix instead of the $ prefix and holds the parameter which holds the string. And that is all.


<?
		//
//testing if var from flash are defined and have contents
if (!isset($myName) || empty($myName)) {
	fail("ERROR: Please select a name!");
}
		//
//function to add or delete information
switch($action) {
    case "send":
        signup($myName, $myCheckbox);
        break;    
    default:
        fail("ERROR: Unknown action: $action");
}
		//
// main function to process the data
function  signup($myName, $myCheckbox) {
	success("NAME: $myName \nCKECKBOX: $myCheckbox");
}
		//
//function in case of failure
function fail($myErmsg) {
    print "&result=$myErmsg";
    exit;
}
		//
//when it was successful...
function success($mySuccess) {
    print "&result=$mySuccess";
    exit;
}
?>