Pin Me

Actionscript Tutorial: Clear Variable on Pressing Search Button

written by: S. R. Obbayi•edited by: Amy Carson•updated: 3/10/2011

When using search forms, variable data related problems can arise. It therefore becomes necessary to handle this problem in a subtle way. You can use ActionScript to clear a variable on pressing search button. Find out how.

  • slide 1 of 4

    Introduction

    Not only can ActionScript clear variables on pressing search button but ActionScript can also refresh data on pressing search button as well as many other form related actions. So why is it important for me to clear variables or refresh data on pressing the search button? Well a known problem as double submitting of data when a user thinks they must double click everything on a web page for it to respond. Most web based application behave differently from Desktop applications with the latter requiring double clicks for actions. Again it may not be a double submission issue. You may want to simply do housekeeping once the user submits the data upon clicking the search button. Whatever the reason am sure it us worth knowing how to refresh data on pressing the search button.

  • slide 2 of 4

    Getting Started

    ActionScript Clear Data For purposes of this tutorial I will simply take a form that will not actually search for anything but simulate this concept. What it will do is ask the user for his or her name, convey its greetings back to the user and the user will then opt to clear their tracks buy clicking the “simulate search" button and the program will not remember the name of the person it conveyed greetings to. It does not matter that this form cannot search, but the implementation of the clearing of the variable is the same. The two images here show the output of the simulator. The internal structure of the ActionScript code is similar to the code in this DateField tutorial.

  • slide 3 of 4

    The ActionScript Code

    The code below simply defines two functions that respond to input from each of the buttons. If the Greetings button is clicked then it displays the name provided. If the Simulate Search button is clicked it clears the Variable in the test field and also the data that is displayed as the greeting. I will explain the code as we go along.

    This line defines the internal data as a string of character.

    var mytxt:String;

    This is the definition of the function that responds to the click of the Greetings button.

    sayhi_button.onRelease = function() {

    This line loads the name typed in the text field into the variable for the character string data.

    mytxt = message_name.text;

    This line checks to see if the character string is empty.

    if(mytxt == "")

    {

    If the string is empty then the simulator dies not know your name

    message_response.text = " Umm, Remind me Your Name ";

    }

    else

    {

    The character string is not empty so we can print out the greeting.

    message_response.text = "Hello, Nice to Meet You " + mytxt;

    }

    };

    This function defines what happens when the Simulate Search button is clicked

    search_button.onRelease = function() {

    This line clears the textfield variable

    message_name.text = "";

    This line refreshes the character string data by making it the same as the textfield variable

    mytxt = message_name.text;

    };

    You an download a copy of the ActionScript Flash executable here to see the simulation in action. This is basically how you can use ActionScript to clear a variable on pressing a search button.

  • slide 4 of 4

    References

    Source: author’s own experience.

    Screenshots provided by writer