0% found this document useful (0 votes)
17 views9 pages

HTML Forms

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 9

Integration with 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 >

<form action="process_form.php" onsubmit="return ValidateFirstName();">


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>

How to Dynamically Add and Delete Form Items

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 ()
{

var radio = document.getElementById("iLikeJs");

if (radio.checked)

document.getElementById("moreCheckboxes").innerHTML = "<input
type='checkbox' name='why' value='because' />";

else

document.getElementById("moreCheckboxes").innerHTML = "";
}

</script>

</head>

<body >

<form action="process_form.php" onsubmit="return ValidateFirstName();">


First name:<br>
<input type="text" id="first_name">
<br>
Last name:<br>
<input type="text" id="last_name""> <br>

Do you like JavaScript?<br>


<input type="radio" id="iLikeJs" name="likeJavascript" value="yes"
onclick="addCheckboxes()">

<input type="radio" name="likeJavascript" value="no"


onclick="addCheckboxes()"><br>
<div id="moreCheckboxes"> </div>
<br><br>
<input type="submit" value="Submit">
</form>

</body>
</html>

Dynamically Working with Select Boxes

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)

var option1 = document.createElement("option");

var option2 = document.createElement("option");

option1.text = "Because I like it.";

option1.value = "because";

option2.text = "Because it is dynamic.";

option2.value = "dynamic";

document.getElementById("selectBoxOptions").add(option1);

document.getElementById("selectBoxOptions").add(option2);

</script>

</head>

<body >

<form action="process_form.php" onsubmit="return ValidateFirstName();">


First name:<br>
<input type="text" id="first_name">
<br>
Last name:<br>
<input type="text" id="last_name""> <br>
Do you like JavaScript?<br>
<input type="radio" id="iLikeJs" name="likeJavascript" value="yes"
onclick="addOptions()">

<input type="radio" name="likeJavascript" value="no"


onclick="addOptions()"><br>
<select id="selectBoxOptions"> </select>
<br><br>
<input type="submit" value="Submit">
</form>

</body>
</html>

You might also like