Jamey Phillips Jamey Phillips - 5 months ago 9
HTML Question

I have a form with the submit button linked to javascript function but nothing is happening when I click the submit button

I'm trying to make a form which when I click the submit button will send the information from the form fields to a javascript function and then show the results to the user. I've made something like this before that's very similar and works fine, but for some reason this isn't doing anything when I click submit. I must have missed something. Here's the code:

<!doctype html>
<html>
<head>
<meta charset="UTF-8" >
<title>Javascript Form</title>
</head>

<body>

<form id="formID" name="myform" action="" method="get">

Customer Name:
<input type="text" name="names" value="">
<br/>

Street Address:
<input type="text" name="street" value="">
<br/>

City:
<input type="text" name="city" value="">
<br/>

State:
<input type="text" name="state" value="">
<br/>

Zip Code:
<input type="text" name="zip" value="">
<br/>

Beginning Odometer Reading:
<input type="text" name="beginO" value="">
<br/>

Ending Odometer Reading:
<input type="text" name="edinO" value="">
<br/>

Number of Days Car was Used:
<input type="text" name="days" value="">
<br/>

<input type="button" name="button" value="Submit" onClick="car(this.form)">
</form>

<script type="text/javascript">
function car(form) {
var names = form.names.value;
var street = form.street.value;
var city = form.city.value;
var state = form.state.value;
var zip = form.zip.value;
var beginO = form.beginO.value;
var endO = form.endO.value;
var days = form.days.value;

var miles = endO - beginO;
var charge = days * 15 + miles * 0.12;

alert("Miles driven: " + miles + ". Charge: " + charge + ". Name: " + names + ". Address : " + street + " , " + city + " " + state + " " + zip + ". ");
}
</script>
</body>
</html>

Answer

It's because you have a JavaScript error at this line:

var endO = form.endO.value;

You have a typo in your form element name:

<input type="text" name="edinO" value="">

form.endO is undefined, so getting value throws an error.

Comments