HTML Forms
HTML Forms
HTML Forms
HTML forms are the objects used to take template information from a user.
For instance, if you work with customers, you need a customer's first and
last name, address, and zip code.
A form object displays input fields for the user to enter this information and
send it back to your web server.
You can use VBScript to provide dynamic customizations to a standard
HTML form.
This lesson explains some ways that VBScript is useful when using forms in
your web pages and how to handle events and validations before submitting
the form data back to your server-side scripts.
What is a Form?
An HTML form is any text box, check box, or radio button that takes input
from a user.
HTML comes with several form input types that you can use without
creating your own input options.
You can customize these form elements using CSS, JavaScript or VB Script
inline HTML tag properties.
Most webmasters need some customizations to their forms, and this is where
VBScript comes in handy.
For instance, suppose you want to ensure that the user has entered a value
into a "first_name" text box before you send the input to the server.
You could send the input to the server, process the data, find the missing
input variable, and then send the form back to the user with an error
message.
Using VBScript with Forms
Let's take a simple form that asks the user to input a first and last name. Take a
look at the code below.
<!DOCTYPE html>
<html>
<head>
</head>
<body >
<form action="process_form.php">
First name:<br>
<input type="text" id="first_name">
<br>
Last name:<br>
<input type="text" id="last_name"">
<br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
Let's add the "onsubmit" event handler and hook into the current HTML page.
Below is a complete copy of our user input form page.
<!DOCTYPE html>
<html>
<head>
<script>
function ValidateFirstName ( )
{
var firstName = document.getElementById("first_name");
if (firstName.value == "")
return false;
else
return true;
}
}
</script>
</head>
<body >
</body>
</html>
We've just seen how to use form input text boxes and validation through VBScript,
but forms usually have other types of input such as radio buttons and check boxes.
You can dynamically add and delete any form element within your script. This is
typically done when you want to take additional information from a user after they
enter other input.
For instance, suppose once a user chooses "Yes" in a radio button group, you then
display check boxes for additional information.
Let's change our form to display two radio buttons that display check boxes when
the user chooses "Yes."
<!DOCTYPE html>
<html>
<head>
<script>
function ValidateFirstName ( )
{
var firstName = document.getElementById("first_name");
if (firstName.value == "")
return false;
else
return true;
}
}
function addCheckboxes ()
{
if (radio.checked)
document.getElementById("moreCheckboxes").innerHTML = "<input
type='checkbox' name='why' value='because' />";
else
document.getElementById("moreCheckboxes").innerHTML = "";
}
</script>
</head>
<body >
</body>
</html>
We talked about dynamically working with elements as you receive user input, and
one common form element used with JavaScript and dynamic loading is the select
box.
The select box is the drop-down element that displays a number of values. From
the list, the user can choose one option.
What if you have hundreds of options, but you only want to show a few options to
the user based on some kind of input.
This is common when you have too many drop-down values to display to the user,
so it could be too frustrating for the user to find a specific element.
<!DOCTYPE html>
<html>
<head>
<script>
function ValidateFirstName ( )
{
var firstName = document.getElementById("first_name");
if (firstName.value == "")
return false;
else
return true;
}
}
function addOptions ()
{
document.getElementById("selectBoxOptions").options.length = 0;
if (radio.checked)
option1.value = "because";
option2.value = "dynamic";
document.getElementById("selectBoxOptions").add(option1);
document.getElementById("selectBoxOptions").add(option2);
</script>
</head>
<body >
</body>
</html>