Help - Search - Members - Calendar
Full Version: Form Scripting Problem
HTMLHelp Forums > Programming > Client-side Scripting
Thomp
On a website I'm doing for the paintball park I work at, I've had to do some private group booking forms through paypal, and I edited the forms to provide more information. That all is working fine; what I'm having trouble with is the scripting I'm trying to use to make sure people fill all the blanks in. On the first form I did, I messed around with it for a while and finally found, because of the paypal system, that I had to put the script in the form tag in the onsubmit function. What I did is,

<form onsubmit="

if (name0.value=='') {
alert('Please Enter a _______');
return false;
}

if (name1.value=='') {
alert('Please Enter a _______');
return false;
}

and so on, with an if statement for each of the other text inputs on the page; what I called "name" in the sample being the name of the blank according to paypal's system, and the long underscore in the alert being what the blank is asking for.

This worked fine on the first page I tried it on, and on three more pages I copied and pasted the whole page's code from and made small changes to it. Then, when I was doing a similar form on a different part of the site a few days later, I tried this same method, but it wouldn't work; the script would be bypassed and the form would submit as normal. At first I thought it might be something in the head tags that I had taken for granted on the working pages. I checked the script tags but they were identical, both the working and non-working pages' tags containing some scripting for rollover images and a popup window. The style tags naturally weren't the same, but I don't see how that could cause my problem. I couldn't find anything on the pages that either wasn't the same or I thought could cause the problem on the non-working page. With some more messing around, I found that the new page would work if I only put one if statement in the form tag, but if I added one more it would stop working again.

Here are the two pages;

working:
http://www.tfergdesign.com/basicbook.html

non-working:
http://www.tfergdesign.com/ezplregistration.html

Can anyone see any differences in the code that would cause the second form's script not to work?

note: if you look in the source code you'll see that currently the working page's script tag has a function for a popup window and the non-working page's doesn't; just in case, I tried putting the popup function on the non-working page, but it didn't make a difference.


pandy
Turn on debugging in your browsers. It's useful. IE's debugger tells me "Line 77, Error: expected ')'".

Line 77
CODE
alert('Please Enter Captain's Name');


You need to escape the apostrophe thusly: Captain\'s. JS thinks the string ends there and thus expects a closing parenthesis. You have several of those.
Thomp
Ahhhhh I would have never realized that. Thank you so much!
Brian Chandler
If you have the same (or similar) checking function in more than one place, it's vastly neater, instead of writing it all out inside the <form > tag to write a function (called checkinput() or similar); then you just write a function call for the onsubmit handler.
Thomp
Thanks for the tip!
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2010 Invision Power Services, Inc.