AS3 help input fields and button capture
Copy link to clipboard
Copied
I created a web banner with 3 input textfields and a button. I need for the the button to capture the input text and build onto a URL.
The ad needs to build a url using the information put in the form fields a url similar to:
PLEASE HELP!!!!
Copy link to clipboard
Copied
What do you have so far?
Copy link to clipboard
Copied
Right now the 3 fields are Address, Apt # and Zip. Created purely by AS3. Below is all I have right now.
Text field Code:
function setInput(inst:TextField, dy, txt):void
{
inst.type = "input";
inst.x = 50;
inst.y = dy;
inst.width = 200;
inst.height = 25;
inst.border = true;
inst.borderColor = 0xcccccc;
inst.maxChars = 50;
inst.backgroundColor = 0xeeeeee;
inst.background = true;
inst.text = txt;
addChild(inst);
}
function setFIn(focus:FocusEvent):void
{
(focus.target as TextField).backgroundColor = 0xffffff;
}
function setFOut(focus:FocusEvent):void
{
(focus.target as TextField).backgroundColor = 0xffffff;
}
var txt_inp1:TextField = new TextField();
var txt_inp2:TextField = new TextField();
var txt_inp3:TextField = new TextField();
var txt_inp4:TextField = new TextField();
var myFormat:TextFormat = new TextFormat();
myFormat.size = 14;
myFormat.leftMargin = 10;
myFormat.align = TextFormatAlign.LEFT;
myFormat.font = "Verdana";
txt_inp1.textColor = 0x999999;
txt_inp2.textColor = 0x999999;
txt_inp3.textColor = 0x999999;
txt_inp4.textColor = 0x999999;
txt_inp1.defaultTextFormat = myFormat;
txt_inp2.defaultTextFormat = myFormat;
txt_inp3.defaultTextFormat = myFormat;
txt_inp4.defaultTextFormat = myFormat;
txt_inp1.addEventListener(FocusEvent.FOCUS_IN, setFIn);
txt_inp1.addEventListener(FocusEvent.FOCUS_OUT, setFOut);
txt_inp2.addEventListener(FocusEvent.FOCUS_IN, setFIn);
txt_inp2.addEventListener(FocusEvent.FOCUS_OUT, setFOut);
txt_inp3.addEventListener(FocusEvent.FOCUS_IN, setFIn);
txt_inp3.addEventListener(FocusEvent.FOCUS_OUT, setFOut);
txt_inp4.addEventListener(FocusEvent.FOCUS_IN, setFIn);
txt_inp4.addEventListener(FocusEvent.FOCUS_OUT, setFOut);
setInput(txt_inp1, 100, 'Address');
setInput(txt_inp2, 133, 'Apt/Condo #');
setInput(txt_inp3, 165, 'Zip Code');
setInput(txt_inp4, 265, '5030671');
Button code:
var goButton:SimpleButton = new SimpleButton();
var myButtonSprite:Sprite = new Sprite();
myButtonSprite.graphics.lineStyle(1, 0x2D8DBD);
myButtonSprite.graphics.beginFill(0x36A3DA,1);
myButtonSprite.graphics.drawRoundRect(50,200,200,30,10);
myButtonSprite.graphics.endFill();
goButton.overState = goButton.downState = goButton.upState = goButton.hitTestState = myButtonSprite;
addChild(goButton);
var tf:TextFormat = new TextFormat();
tf.color = 0xFFFFFF;
tf.font = "Verdana";
tf.size = 14;
tf.align = "center";
var txt:TextField = new TextField();
txt.text = "SHOW OFFERS";
txt.x = 0;
txt.y = 0;
txt.width = myButtonSprite.width;
txt.height = myButtonSprite.height;
txt.setTextFormat( tf );
var mc:MovieClip = new MovieClip();
mc.addChild( myButtonSprite );
mc.addChild( txt );
goButton.addEventListener(MouseEvent.CLICK,goThere);
function goThere(e:MouseEvent){
var request:URLRequest = new URLRequest("http://direct.digitallanding.com/dispatch.aspx?");
navigateToURL(request);
}
Copy link to clipboard
Copied
It's hard to give you specific help, since you haven't given your fields descriptive variable names. There's no way to know which field is being used for what. So I can really do is suggest you look at the example for URLVariables.
Note that since you're using timeline code, you'll probably be better off just putting your textfields on the stage and cutting all the addChild, x, and y stuff. If you want to do all that, you should probably go with a Document Class instead.
Copy link to clipboard
Copied
Thanks.
txt_inp1 is for 'Address'; txt_inp2 is for 'Apt/Condo #'; txt_inp3 is for 'Zip Code'...
If i'm doing this the long way and there's a simpler route please let me know. I just really need to know what steps I need to take to make the info put into the fields gets added to this link (http://direct.digitallanding.com/dispatch.aspx?) building a URL similar to this: http://order.comcastauthorizedoffers.com/dispatch.aspx?address=5715%20 hereford&zip=48224&featuredc...
Copy link to clipboard
Copied
What does your code look like after you incorporated the URLVariables example?
Copy link to clipboard
Copied
This is the code I had applied tmporarily yesterday.
import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.net.URLVariables;
import flash.net.URLRequestMethod;
import flash.events.Event;
var url : String = 'http://direct.digitallanding.com/dispatch.aspx?';
var urlVariables : URLVariables = new URLVariables ();
urlVariables['txt_inp1'] = 'varvalue';
urlVariables['txt_inp2'] = 'varvalue1';
urlVariables['txt_inp3'] = 'varvalue2';
urlVariables['txt_inp4'] = 'varvalue3';
var request : URLRequest = new URLRequest ( url );
request.data = urlVariables;
request.method = URLRequestMethod.GET;
Copy link to clipboard
Copied
You should be good to go if you replace the "varvalue" with txt_inp1.text. Note that the back end will receive variables entitled txt_inp1, etc. If that's not what you want to send, then edit the left side as well.
Copy link to clipboard
Copied
I've reworked my input field and button code a bit but i still can't get my button to capture the address, apt# & zip code, add it to this url http://direct.digitallanding.com/dispatch.aspx? and open the necessary page. I've tried using the variables but its just not working.
Can I get an example of what i need to do?!
Copy link to clipboard
Copied
What code do you have now?
Copy link to clipboard
Copied
As of now:
Text field Code:
function setInput(inst:TextField, dy, txt):void
{
inst.type = "input";
inst.x = 50;
inst.y = dy;
inst.width = 200;
inst.height = 25;
inst.border = true;
inst.borderColor = 0xcccccc;
inst.maxChars = 50;
inst.backgroundColor = 0xeeeeee;
inst.background = true;
inst.text = txt;
addChild(inst);
}
function setFIn(focus:FocusEvent):void
{
(focus.target as TextField).backgroundColor = 0xffffff;
}
function setFOut(focus:FocusEvent):void
{
(focus.target as TextField).backgroundColor = 0xffffff;
}
//Address Textfield
var address:TextField=new TextField();
addChild(address);
address.type='input';
address.border=true;
address.text='Address';
address.restrict = "A-Z a-z 0-9";
address.addEventListener(TextEvent.TEXT_INPUT,function(){trace('input')});
address.addEventListener(FocusEvent.FOCUS_IN,function(){address.text=''});
address.addEventListener(FocusEvent.FOCUS_OUT,function(e:FocusEvent){
e.currentTarget.text=(e.currentTarget.text=='')?'Address':e.currentTarget.text;
});
//Apt/Condo # Textfield
var apt:TextField=new TextField();
addChild(apt);
apt.type='input';
apt.border=true;
apt.text='Apt/Condo #';
apt.restrict = "A-Z a-z 0-9";
apt.addEventListener(TextEvent.TEXT_INPUT,function(){trace('input')});
apt.addEventListener(FocusEvent.FOCUS_IN,function(){apt.text=''});
apt.addEventListener(FocusEvent.FOCUS_OUT,function(e:FocusEvent){
e.currentTarget.text=(e.currentTarget.text=='')?'Apt/Condo #':e.currentTarget.text;
});
//Zip Textfield
var zip:TextField=new TextField();
addChild(zip);
zip.type='input';
zip.border=true;
zip.text='Zip Code';
zip.restrict = "0-9";
zip.addEventListener(TextEvent.TEXT_INPUT,function(){trace('input')});
zip.addEventListener(FocusEvent.FOCUS_IN,function(){zip.text=''});
zip.addEventListener(FocusEvent.FOCUS_OUT,function(e:FocusEvent){
e.currentTarget.text=(e.currentTarget.text=='')?'Zip Code':e.currentTarget.text;
});
//Promo ID Textfield
var promo:TextField = new TextField();
promo.text='5030671';
promo.addEventListener(FocusEvent.FOCUS_IN, setFIn);
promo.addEventListener(FocusEvent.FOCUS_OUT, setFOut);
var myFormat:TextFormat = new TextFormat(); // Essential - Textformatting settings
myFormat.size = 14; // Setting fontsize to 14px
myFormat.leftMargin = 10; // Setting right margin to 20px
myFormat.align = TextFormatAlign.LEFT; // Setting text-align to left
myFormat.font = "Verdana"; // Setting font to Verdana
address.textColor = 0x999999;
apt.textColor = 0x999999;
zip.textColor = 0x999999;
promo.textColor = 0x999999;
address.defaultTextFormat = myFormat; // Connecting textfield with textformat
apt.defaultTextFormat = myFormat;
zip.defaultTextFormat = myFormat;
promo.defaultTextFormat = myFormat;
setInput(address, 100, 'Address');
setInput(apt, 133, 'Apt/Condo #');
setInput(zip, 165, 'Zip Code');
setInput(promo, 265, '5030671');
Button Code:
import fl.controls.Button;
var tf:TextFormat = new TextFormat();
tf.color = 0xFFFFFF;
tf.font = "Verdana";
tf.size = 14;
tf.bold = true;
var goButton:Button = new Button();
goButton.label = "SHOW OFFERS";
goButton.setSize(240, 30);
goButton.move(30, 200);
goButton.setStyle("textFormat", tf);
goButton.addEventListener(MouseEvent.CLICK,goThere);
function goThere(e:MouseEvent){
var request:URLRequest = new URLRequest("http://direct.digitallanding.com/dispatch.aspx?");
navigateToURL(request);
}
addChild(goButton);
Variables:
package{
import flash.display.Sprite;
import flash.net.URLRequest;
import flash.net.navigateToURL;
import flash.net.URLVariables;
import flash.events.Event;
public class URLVariables extends Sprite {
public function URLVariables() {
var url : String = 'http://direct.digitallanding.com/dispatch.aspx?';
var request : URLRequest = new URLRequest ( url );
var urlVariables : URLVariables = new URLVariables ();
urlVariables['address'] = 'address.text';
urlVariables['apt'] = 'apt.text';
urlVariables['zip'] = 'zip.text';
urlVariables['promo'] = 'promo.text';
request.data = urlVariables;
}
}
}
Copy link to clipboard
Copied
Do you have strict mode on on your publish settings? I don't think this should compile.
You also should call your Class something other than URLVariables, because it's not clear if you're recursively instantiating your class inside itself or if you're instantiating a flash.net.URLVariables.
I don't think your URLVariables Class has a reference to what looks like frame script code, so it won't know what your address text field, etc. contain. I think until you get it working without breaking out a Class, you should just put it all in a frame script. It seems to me you're not ready for dependency injection yet.
Finally, when you wrap 'address.text' in quotes, the string contains 'address.text', not the contents of the address text field.
Copy link to clipboard
Copied
Current Code:
package actions {
/*
always extend a class using movieclip instead of sprite when using flash.
*/
import flash.display.MovieClip;
import flash.events.*;
import flash.net.*;
public class main extends MovieClip {
public function main():void {
/*
buttonMode gives the submit button a rollover
*/
//submit_button.buttonMode = true;
submit_button.addEventListener(MouseEvent.MOUSE_DOWN, validateForm);
/*adwords code*/
submit_button.addEventListener(MouseEvent.CLICK,
function(event: MouseEvent) : void {flash.net.navigateToURL(new URLRequest( root.loaderInfo.parameters.clickTAG), "_blank");
}
);
/*
set the initial textfield values
*/
address.text = "Address";
apt.text = "Apt/Condo #";
zip.text = "Zip";
address.addEventListener(MouseEvent.MOUSE_DOWN, clearAddressInput);
apt.addEventListener(MouseEvent.MOUSE_DOWN, clearAptInput);
zip.addEventListener(MouseEvent.MOUSE_DOWN, clearZipInput);
address.addEventListener(FocusEvent.FOCUS_OUT, addressInputPlaceholder);
apt.addEventListener(FocusEvent.FOCUS_OUT, aptInputPlaceholder);
zip.addEventListener(FocusEvent.FOCUS_OUT, zipInputPlaceholder);
}
public function addressInputPlaceholder(e:FocusEvent):void {
if (address.text == "") {
address.text = "Address";
address.textColor = 0x666666;
}
}
public function aptInputPlaceholder(e:FocusEvent):void {
if (apt.text == "") {
apt.text = "Apt/Condo #";
apt.textColor = 0x666666;
}
}
public function zipInputPlaceholder(e:FocusEvent):void {
if (zip.text == "") {
zip.text = "Zip";
zip.textColor = 0x666666;
}
}
public function clearAddressInput(e:MouseEvent):void {
if (address.text == "Address" || address.text == "Enter your address") {
address.text = "";
address.textColor = 0x000000;
}
}
public function clearAptInput(e:MouseEvent):void {
if (apt.text == "Apt/Condo #" || apt.text == "Enter your apt/condo #") {
apt.text = "";
apt.textColor = 0x000000;
}
}
public function clearZipInput(e:MouseEvent):void {
if (zip.text == "Zip" || zip.text == "Enter your zip") {
zip.text = "";
zip.textColor = 0x000000;
}
}
public function validateForm(e:MouseEvent):void {
/*
check fields
*/
if (address.text == "" || apt.text == "" || zip.text == "" ||
address.text == "Address" || apt.text == "Apt/Condo #" || zip.text == "Zip" ||
address.text == "Enter your address" || apt.text == "Enter your apt/condo #" || zip.text == "Enter your zip") {
if (address.text == "" || address.text == "Address" || address.text == "Enter your address") {
address.text = "Enter your address";
}
if (apt.text == "" || apt.text == "Apt/Condo #" || apt.text == "Enter your apt/condo #") {
apt.text = "Enter your apt/condo #";
}
if (zip.text == "" || zip.text == "Zip" || zip.text == "Enter your zip") {
zip.text = "Enter your zip";
}
} else {
/*
init function to process form
*/
processForm();
}
}
/*
function to process our form
*/
public function processForm():void {
navigateToURL(new URLRequest("http://direct.digitallanding.com/dispatch.aspx?address=" + address.text + "&apt=" + apt.text + "&zip=" + zip.text + "&promoid=5050101" + "&featuredcategory=1"), "_blank");
}
}
}

