/    Sign up×
Community /Pin to ProfileBookmark

Javascript Form error

Hello, is there anyone who can help me please with a problem i’ve got with the web site ive just taken over at work? I am solely reponsible for the site and can do the basics and little else. We currently have one order form on our site: [url]www.lawreports.co.uk/orderform2002.htm[/url] which links to a service provider called Netbanx. They handle all our orders and credit card payments.
My problem is that this page has not been working since last year, as far as i know due to the fact it hasnt got the coding below inserted anywhere. Would anyone be able to tell me why its not working once ive inserted it onto the page and whereabouts I should insert it? Im working with DW 4. I think theres something wrong with the return function.
Thanks to anyone who takes the time to answer this!! Karen

<script language=”JavaScript”>
<!– Hide script function formCheck()
{
totalAmount = 0;
prodSelected = false;
for (var i=0;i<document.payform.length;i++) {
if (document.payform.elements[i].checked){
totalAmount += eval(document.payform.elements[i].value);
prodSelected = true;
}
}
}
if (prodSelected) {
document.payform.payment_amount.value=totalAmount;
return true;
} else {
alert(‘Please select a product’);
return false;
}
}
// –>
</script>

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@Willy_DuittFeb 17.2005 — Place the script in the HEAD of the document...

This should be on two lines: <!-- Hide script function formCheck()

IE:

<!-- Hide script

function formCheck()

Call the script using the form onsubmit event handler...

IE:

<form onsubmit="return formCheck()"....

.....Willy
Copy linkTweet thisAlerts:
@PittimannFeb 17.2005 — Hi!

It is not only the hiding of the function which 'disturbs'. Here is a little example:[code=php]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Example</title>
<script language="JavaScript" type="text/javascript">
<!--
function formCheck(val){
totalAmount = 0;
prodSelected = false;
for (var i=0;i<document.payform.length;i++) {
if (document.payform.elements[i].checked){
totalAmount += document.payform.elements[i].value-0;
prodSelected = true;
}
}
if (prodSelected) {
document.payform.payment_amount.value=totalAmount.toFixed(2);
return true;
}
else{
document.payform.payment_amount.value='';
if(val){
alert('Please select a product');
return false;
}
}
}
//-->
</script>
</head>
<body>
<form name="payform" action="#" method="" onsubmit="return formCheck(1)">
<input type="checkbox" name="product" value="2" onclick="formCheck()">
<input type="checkbox" name="product" value="3.5" onclick="formCheck()">
<input type="checkbox" name="product" value="7.99" onclick="formCheck()">
<input type="checkbox" name="product" value="12" onclick="formCheck()">
<input type="checkbox" name="product" value="3.2" onclick="formCheck()">
<input type="submit" value="click" onclick="1.1" onclick="formCheck()"><br>
total: <input name="payment_amount">
</form>
</body>
</html>[/code]
Cheers - Pit
Copy linkTweet thisAlerts:
@kazzayauthorFeb 17.2005 — Thanks Willy and Pit! People to talk to at last!!

Willy I have made the corrections that you suggested but its still not working. Have i placed the onsubmit event handler in the right place? Its in the body. Ive put up a test page at http://www.lawreports.co.uk/order4.htm


Im also getting these error messages do they mean anything to you?

line 19 Return statement outside of function

line 177 object expected

Pit, youre example was a bit hard for me to understand Im afraid!

Kaz
Copy linkTweet thisAlerts:
@PittimannFeb 17.2005 — Hi!

OK. Let's get back to your code:

<script language="JavaScript">

<!--

function formCheck()//line break before this, like Willy said

{

totalAmount = 0;

prodSelected = false;

for (var i=0;i<document.payform.length;i++) {

if (document.payform.elements[i].checked){

totalAmount += eval(document.payform.elements[i].value);

prodSelected = true;

----------------remove one of the curly brackets below

}

}

}
----------------



if (prodSelected) {

document.payform.payment_amount.value=totalAmount;

return true;

} else {

alert('Please select a product');

return false;

}

}

// -->

</script>



Then it should work.



Cheers & good luck - Pit
Copy linkTweet thisAlerts:
@kazzayauthorFeb 17.2005 — Hi pit

Thanks very much for your help! I feel like a total idiot but im still not having any luck! Ive done all you said and put the page up again but its still not right and am now getting an error message about line 13 which reads: line 13 "buckram" is undefined. Im utterly confused as buckram isnt mentioned on line 13 anyway. it just doesnt seem to be able to pick up the choices at all!

Sorry about this!

kaz
Copy linkTweet thisAlerts:
@PittimannFeb 17.2005 — Hi!

I just used the link you provided. You are looping through all form elements and look if they are checked. A couple of radio buttons have values which cannot be used in maths. One of them is 'buckram'. Here is a snippet filtering these out:[code=php]<script language="JavaScript" type="text/javascript">
<!--
function formCheck(){
totalAmount = 0;
prodSelected = false;
for (var i=0;i<document.payform.length;i++) {
if (document.payform.elements[i].checked&&!isNaN(document.payform.elements[i].value-0)){
totalAmount += document.payform.elements[i].value-0;
prodSelected = true;
}
}
if (prodSelected) {
document.payform.payment_amount.value=totalAmount;
return true;
} else {
alert('Please select a product');
return false;
}
}
//-->
</script>[/code]
Cheers - Pit
Copy linkTweet thisAlerts:
@kazzayauthorFeb 17.2005 — Hi Pit,

Its worked!!!!!!! Fantastic! I cant say thank you enough for taking the time to look into this for me. Its all working now and Im very relieved!

Kaz
Copy linkTweet thisAlerts:
@PittimannFeb 17.2005 — Hi Kaz,

you are welcome. Nice you got it now! ?

Regards - Pit
×

Success!

Help @kazzay 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.5,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...