Skip to main content
vedtam
Known Participant
May 12, 2011
Answered

Contact Form - field names

  • May 12, 2011
  • 1 reply
  • 549 views

Hi,

I have here a nice contact form. The only problem I got is that I would like to have the field names (name, e-mail, telephone), displayed inside the right field, (so when the user clicks inside, the text would disappear automatically) in stead of displaying the names next to the fields.

My first tough was to write inside each field, in flash the field names, and it was ok for the first look. But the user needs to delete first the field names like "name" from the input box, in order to enter his own.

How could I make these field names automatically dissapear when the user clicks inside a text box?

the code:

stop();
send_btn.addEventListener(MouseEvent.CLICK, submit);

function submit(e:MouseEvent):void
{
var variables:URLVariables = new URLVariables();
variables.fromname = name_txt.text;
variables.fromtel = tel_txt.text;
variables.fromemail = email_txt.text;
variables.frommessage = message_txt.text;

var req:URLRequest = new URLRequest("contact.php");
req.data = variables;
req.method = URLRequestMethod.POST;

var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.addEventListener(Event.COMPLETE, sent);
loader.addEventListener(IOErrorEvent.IO_ERROR, error);
loader.load(req);
status_txt.text = "Sending...";
}

function sent(e:Event):void
{
status_txt.text = "Message Sent.";
name_txt.text = "";
tel_txt.text = "";
email_txt.text = "";
message_txt.text = "";
}

function error(e:IOErrorEvent):void
{
status_txt.text = "Error. Please try lather.";
}

Thanks!!

ved

This topic has been closed for replies.
Correct answer Ned Murphy

Here's one to think about.  The TextEvent.TEXT_INPUT event of a TextField fires before the text that is input is entered into the TextField.  You can use this to your advantage to remove whatever is in the TextField as soon as someone starts typing.  Something like the following would do

name_txt.addEventListener(TextEvent.TEXT_INPUT, eraseLabel);
email_txt.addEventListener(TextEvent.TEXT_INPUT, eraseLabel);

function eraseLabel(evt:TextEvent):void {
   evt.currentTarget.text = "";
   evt.currentTarget.removeEventListener(TextEvent.TEXT_INPUT, eraseLabel);
}

You may have to get creative to restore the labels if there's a need to, but that's another thing to think about.

1 reply

Ned Murphy
Ned MurphyCorrect answer
Legend
May 12, 2011

Here's one to think about.  The TextEvent.TEXT_INPUT event of a TextField fires before the text that is input is entered into the TextField.  You can use this to your advantage to remove whatever is in the TextField as soon as someone starts typing.  Something like the following would do

name_txt.addEventListener(TextEvent.TEXT_INPUT, eraseLabel);
email_txt.addEventListener(TextEvent.TEXT_INPUT, eraseLabel);

function eraseLabel(evt:TextEvent):void {
   evt.currentTarget.text = "";
   evt.currentTarget.removeEventListener(TextEvent.TEXT_INPUT, eraseLabel);
}

You may have to get creative to restore the labels if there's a need to, but that's another thing to think about.

vedtam
vedtamAuthor
Known Participant
May 13, 2011

Hi Ned,

I had something simmilar in mind, but I did not imagine that the solution can be simple as this...Anyway, it is working!!

Thanks!! 

Ned Murphy
Legend
May 13, 2011

You're welcome