/    Sign up×
Community /Pin to ProfileBookmark

onSubmit keeps resetting form.

Hi, I have been searching high and low and I can’t seem to find an answer. I am new at JS and I have created an form and when I click the on submit button it resets all the fields. The fields are always blank after I click reset. I have been working on this for 6 hours.

[CODE]
<head>
<title>H10</title>
<script type=”text/javascript” src=”H10.js”></script>
</head>
<body>
<form name=”HO10″ onSubmit=”calculateOrder()”>

<p>Enter the number of books to order : <input type=”text” id=”numberOfBooksOrdered” size=”5″ /></p>
<p>The total order amount is <span id=”result”>zero</span></p>
<input type=”submit” name=”orderButton” value=”Order”>
<input type=”reset” name=”resetOrder” onclick=”orderReset()” value=”Reset”>
<p>Check to confirm Order: <input id=”nameOfCheckbox” type=”checkbox”></p>
</form>
</body>
here is the JS code
function calculateOrder()
{
var books=document.getElementById(‘numberOfBooksOrdered’).value;
var bookPrice = 17.99;
var discountPercentage = 0.2; // worked out from 20/100
var discountTotal = 0;
var total = 0;

if (books >= 5)
{
var bookTotal = books * bookPrice;
discountTotal = bookTotal * discountPercentage;
total = bookTotal – discountTotal;
}
else if (books < 5 && books > 0)
{
total = books * bookPrice;
}
else
{
alert (“You can’t order zero books”);
}
//replace zero with total
document.getElementById(“result”).innerHTML =(“£” + total);
// checkbok ticked
if (document.getElementById(“nameOfCheckbox”).checked == true)
{
alert (“Thanks for ordering with eBooks. :-)”);
}
}
function orderReset()
{
document.getElementById(‘result’).innerHTML = “zero”;
}
[/CODE]

Apologies about the long code but this is what I have done. It just keeps resetting when I click Submit. I see for a millisecond that the total appears but then disappears along with the amount I put in the input text box. What am I doing wrong and why? I want to know the reason.

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@protonauthorFeb 10.2010 — Sorry at the top I said "The fields are always blank after I click reset" I meant to say that The fields are always reset after I click the submit button".
Copy linkTweet thisAlerts:
@Gozzy82Feb 10.2010 — i tried your code @ http://www.webdevout.net/test?01m

i did not change much but it appears to be working ok.
Copy linkTweet thisAlerts:
@protonauthorFeb 10.2010 — Thanks Gozzy82, but it still doesn't work. I am using a separate JS file.

With [code=html]<script type="text/javascript" src="H10.js"></script>[/code] in the header part of the HTML and the Javascript in a separate JS file.

I am not sure why it works on that site you gave me (nice site though, thanks for that, I'm bookmarking it).

But when I click submit it does the same thing where it displays the results for milliseconds and then clears but on that site it works perfectly. I have been "googleing" or is it googling for answers this whole day and still no luck. ?

I have tried changing the Form tag by including return false and tried even return true and added action="#" and still no luck.

Try cutting and pasting into a separate HTML & JS file and run it to see if you get the same results. I have tried it on IE6 & 8 and Firefox 3.6. One OS was Windows XP Pro (at work) and my Home PC is Windows 7 but it has the same results. I need to know where I am going wrong. Please help.
Copy linkTweet thisAlerts:
@kaafmimFeb 10.2010 — Try adding a
[CODE]return false;[/CODE]
at the end of your function. It stops all the actions done automatically when submitting a form. Hope it helps!
Copy linkTweet thisAlerts:
@protonauthorFeb 10.2010 — Hi kaafmim, that worked, thanks. I found out what I did, my Syntax was wrong, I had the inverted comma before the return false; so it was like this
[code=html]<form name="HO10" onSubmit="calculateOrder();" return false;
[/code]
instead of
[code=html]<form name="HO10" onSubmit="calculateOrder(); return false;">
[/code]
wow nearly 3 days on this problem...

Thanks everyone... at this moment I love you all... HAHA
×

Success!

Help @proton spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...