joedfuse
asked on
Help with checkboxes and arrays in actionscript
Hello experts,
Im stumped.... I am fairly new to action script and need to create a flash form that has 3 columns with a few check boxes in each. Each checkbox contains a prod name and a price.
I need to create a submit function that will add up the selected checboxes and give me a total. As well as a little if then conditional validation to make sure they select the allowed combo of products.
I could probably do this in 10 mins in coldfusion but this has to be completly done in flash with no external file links etc....
I have no problem going through tutorials but i am yet to find one that covers the data array to contain products, the checkbox form and submit function to validate and sum the total.
Thanks for your time
Im stumped.... I am fairly new to action script and need to create a flash form that has 3 columns with a few check boxes in each. Each checkbox contains a prod name and a price.
I need to create a submit function that will add up the selected checboxes and give me a total. As well as a little if then conditional validation to make sure they select the allowed combo of products.
I could probably do this in 10 mins in coldfusion but this has to be completly done in flash with no external file links etc....
I have no problem going through tutorials but i am yet to find one that covers the data array to contain products, the checkbox form and submit function to validate and sum the total.
Thanks for your time
ASKER
Thanks for the response. The least of my probs was the check box but more along the lines of dynamically create the check box from product array then add those values up on a submit button without leaving the flash file.
Thanks
Thanks
I don't know what version of Flash/ActionScript you are using, but the Flash manual has sample syntax you can borrow from... ;)
I don't quite understand what you meant by 'product array' and 'without leaving the flash file' part...
CyanBlue
I don't quite understand what you meant by 'product array' and 'without leaving the flash file' part...
CyanBlue
ASKER
ok heres some javascript to try to explain the babbling nonsense i have going through my head when trying to explain what im looking for. Im using Flash cs4.
In the code snip i have some products and prices loaded. What im looking for is the actionscript version of this array, dynamically create checkboxes based on this viewing only products not prices. Then after a submission button clicked it adds the values up and if the cost under 20 dollars it yells CORRECT other wise it lets people know they fail.
And when i refer to not leave the flash i mean i cant use database, php page, xml page etc... everything needs to be done within flash. If i could use other tech believe me this would all get done in CF.
Im gonna increase the point value of this question to take into account the time actually getting to the question lol
In the code snip i have some products and prices loaded. What im looking for is the actionscript version of this array, dynamically create checkboxes based on this viewing only products not prices. Then after a submission button clicked it adds the values up and if the cost under 20 dollars it yells CORRECT other wise it lets people know they fail.
And when i refer to not leave the flash i mean i cant use database, php page, xml page etc... everything needs to be done within flash. If i could use other tech believe me this would all get done in CF.
Im gonna increase the point value of this question to take into account the time actually getting to the question lol
var prodNameCol1 = new Array(); //product name col 1
prodNameCol1[0] = "Prod 1";
prodNameCol1[1] = "Prod 2";
prodNamecol1[2] = "Prod 3";
var priceCol1 = new Array(); //prices of products col 1
priceCol1[0] = 2.00;
priceCol1[1] = 2.00;
priceCol1[2] = 2.95;
var prodNameCol2 = new Array(); //product name col 2
prodNameCol2[0] = "Prod 4";
prodNameCol2[1] = "Prod 5";
prodNamecol2[2] = "Prod 6";
var priceCol1 = new Array(); //prices of products col 2
priceCol2[0] = 2.00;
priceCol2[1] = 2.00;
priceCol2[2] = 2.95;
I do understand the question except the submit part of it... So, there is no actual call to an external script to post it to database or something, but rather you simply want to display the total of it, I guess... Am I correct on that???
Why don't you take a stab at it and see where get??? It seems fairly straight forward to me... Honestly, I don't plan to do that for you, but I can help you get through it if you get stuck while you try...
CyanBlue
Why don't you take a stab at it and see where get??? It seems fairly straight forward to me... Honestly, I don't plan to do that for you, but I can help you get through it if you get stuck while you try...
CyanBlue
ASKER
Thats the problem i dont know where to start looking Im new to action script an am looking for tutorials that cover what im trying to do. Im not asking for anyone to write the code for me just point me in a direction to write the array that im looking for and a tutorial to get the SUM as well as apply some conditional statements.
There can be multiple tutorials that tie this together. And believe me im looking as well as i post this.
I wrote an example again below in CF this works fine, just dont know how to translate to actionscript.
There can be multiple tutorials that tie this together. And believe me im looking as well as i post this.
I wrote an example again below in CF this works fine, just dont know how to translate to actionscript.
<!--- Build a new query with name and cost. --->
<cfset qProducts = QueryNew( "name, cost" ) />
<!--- Add some rows to the query. --->
<cfset QueryAddRow( qProducts ) />
<cfset qProducts[ "name" ][ 1 ] = "Prod1" />
<cfset qProducts[ "cost" ][ 1 ] = "2.00" />
<cfset QueryAddRow( qProducts ) />
<cfset qProducts[ "name" ][ 2 ] = "Prod2" />
<cfset qProducts[ "cost" ][ 2 ] = "2.00" />
<cfset QueryAddRow( qProducts ) />
<cfset qProducts[ "name" ][ 3 ] = "Prod3" />
<cfset qProducts[ "cost" ][ 3 ] = "2.00" />
<!--- Get the sum of costs. --->
<cfset flcostSum = ArraySum( qProducts["cost"] ) />
<cfoutput>#flcostSum#</cfoutput>
This is the easiest code sample I can think of which should work well with AS3...
Create a new FLA file... Drag a Checkbox component from the Components panel onto the stage... Select that instance from the stage and delete it...
Paste this code into the frame 1 of your movie and test it...
CyanBlue
Create a new FLA file... Drag a Checkbox component from the Components panel onto the stage... Select that instance from the stage and delete it...
Paste this code into the frame 1 of your movie and test it...
CyanBlue
import fl.controls.CheckBox;
var products:Array = new Array();
products.push({name:"Prod 1", cost:"2.00"});
products.push({name:"Prod 2", cost:"2.00"});
products.push({name:"Prod 3", cost:"2.95"});
products.push({name:"Prod 4", cost:"2.00"});
products.push({name:"Prod 5", cost:"2.00"});
products.push({name:"Prod 6", cost:"2.95"});
setupCheckBoxes();
function setupCheckBoxes()
{
for (var i:Number = 0; i < products.length; i++)
{
this["cb" + i] = new CheckBox();
this["cb" + i].label = products[i].name + " : " + products[i].cost;
this["cb" + i].name = "prod" + i;
this["cb" + i].addEventListener(MouseEvent.CLICK, updateCost);
addChild(this["cb" + i]);
this["cb" + i].y = i * 20;
}
}
function updateCost(e:MouseEvent):void
{
var cb:CheckBox;
var idx:Number = 0;
var sum:Number = 0;
for (var i:Number = 0; i < products.length; i++)
{
cb = getChildByName("prod" + i) as CheckBox;
if (cb.selected == true)
{
sum += parseFloat(products[i].cost);
}
}
trace(sum);
}
ASKER
thanks for the response... there where about 8 errors in the code just put in so i made some changes to the code and now only getting one nagging error
1086: Syntax error: expecting semicolon before dot.
Its happening in Line 1.
I'm assuming it has something to do with cost but not sure.
1086: Syntax error: expecting semicolon before dot.
Its happening in Line 1.
I'm assuming it has something to do with cost but not sure.
var products.push(name:"Prod 6", cost:"2.95");
setupCheckBoxes();
function setupCheckBoxes()
{
for (var i:Number = 0; i < products.length; i++)
{
this["cb" + i] = new CheckBox();
this["cb" + i].label = products[i].name + " : " + products[i].cost;
this["cb" + i].name = "prod" + i;
this["cb" + i].addEventListener(MouseEvent.CLICK, updateCost);
addChild(this["cb" + i]);
this["cb" + i].y = i * 20;
}
}
function updateCost(e:MouseEvent):void
{
var cb:CheckBox;
var idx:Number = 0;
var sum:Number = 0;
for (var i:Number = 0; i < products.length; i++)
{
cb = getChildByName("prod" + i) as CheckBox;
if (cb.selected == true)
{
sum += parseFloat(products[i].cost);
}
}
trace(sum);
}
You cannot push anything unless you declare an array first... In other words, you are missing this line...
var products:Array = new Array();
CyanBlue
var products:Array = new Array();
CyanBlue
ASKER
you rock!
now i just want to add comments to your code to see if i am properly understanding what you did as im trying to learn this and not just throw some code in an app and call it a day.
as you can see from my code i added comments seeing if i can explain what you did. Now lets say i wanted a button to do the sum in another frame and dynamic txt box.. i would just take listener out of the setupcheckbox function correct? and that buttons function would be where i would do validation?
thanks for all you help i will be accepting this solution
now i just want to add comments to your code to see if i am properly understanding what you did as im trying to learn this and not just throw some code in an app and call it a day.
as you can see from my code i added comments seeing if i can explain what you did. Now lets say i wanted a button to do the sum in another frame and dynamic txt box.. i would just take listener out of the setupcheckbox function correct? and that buttons function would be where i would do validation?
thanks for all you help i will be accepting this solution
//get the checkbox control from lib
import fl.controls.CheckBox;
// create the array and populate itwith a prod name and cost
var products:Array = new Array();
products.push({name:"Prod 1", cost:"2.00"});
products.push({name:"Prod 2", cost:"2.00"});
products.push({name:"Prod 3", cost:"2.95"});
products.push({name:"Prod 4", cost:"2.00"});
products.push({name:"Prod 5", cost:"2.00"});
products.push({name:"Prod 6", cost:"2.95"});
//build checkboxes onscreen at y coord from top of screen
setupCheckBoxes();
function setupCheckBoxes()
{
//LOOP through array to build checkboxes
for (var i:Number = 0; i < products.length; i++)
{
this["cb" + i] = new CheckBox();// call a new instance of checkbox
this["cb" + i].label = products[i].name;//write the labels to screen
this["cb" + i].name = "prod" + i;//assign product name to label
this["cb" + i].addEventListener(MouseEvent.CLICK, updateCost);
addChild(this["cb" + i]); //tell flash to update everytime mouse clicks checkbox
this["cb" + i].y = i * 20; //checkbox position
}
//function that runs in the loop when mouse event happens
function updateCost(e:MouseEvent):void
{
var cb:CheckBox;//set cb as variable of checkbox component
var idx:Number = 0;//clear the index for array
var sum:Number = 0;//clear variable sum
for (var i:Number = 0; i < products.length; i++) //loop through array
{
cb = getChildByName("prod" + i) as CheckBox;//get the products from the looped array
if (cb.selected == true)
{
sum += parseFloat(products[i].cost);//if the checkbox is true add the cost to the sum
}
}
trace(sum);//test app
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I have some more positioning questions but i will be posting another thread as i need to gather my thoughts
http://www.flashscript.biz/php/checkbox/checkbox.html
CyanBlue