Forum Moderators: open

Message Too Old, No Replies

Need help with a JavaScript checkbox validator

Needs to pass URL through

         

jdkuehne

8:57 pm on Jul 8, 2008 (gmt 0)

10+ Year Member



I've not been able to find a script that performs this function and my coding ability is poor. I would like to have the user click on a page link and be taken to an intermediate page where he would have to click a checkbox and click a button to continue to the destination. If he doesn't check the box, an alert box should pop up. Ideally, this should only happen once per session which I know would require a cookie to be set. I know this could be done with a CGI Script, but there are other reasons I'd like the user to have enabled JavaScript.

If anyone can help me out, or at least point me to a resource which might help, I would be grateful. Thanks.

coopster

1:09 pm on Jul 9, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



One option would be to have an onclick event handler set up for the button that fires off a function. This is off the top of my head, but hopefully will get you started ...
function didYouCheckIt() 
{
if (!document.getElementById('myCheckBox').checked) {
alert('You must check the box.')
return false
}
return true
}
}

eelixduppy

2:35 pm on Jul 9, 2008 (gmt 0)



Just note there is an extra closing bracket in there at the end that should be removed :)

rocknbil

5:54 pm on Jul 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



click a checkbox and click a button to continue to the destination.

Also, no reason to return true. Return false stops the form from performing it's normal function onSubmit, so you want to let the Javascript manage the submit:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<!-- doctype all on one line -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Checkbox</title>
<script type="text/javascript">
function didYouCheckIt() {
if (! document.getElementById('myCheckBox').checked) {
alert('You must check the box.')
}
else { form.submit(); }
return false;
}
</script>
</head>
<body>
<form method="get" action="http://www.example.com" onSubmit="return didYouCheckIt();">
<input type="checkbox" name="myCheckBox" id="myCheckBox"> I have read the TOS
<input type="submit" value="Go">
</form>
</body>
</html>

jdkuehne

8:17 pm on Jul 9, 2008 (gmt 0)

10+ Year Member



Thanks for the replies.

Rocknbil, Your script looks like it is going to set me on the proper path. I think I can figure out how to set the session cookie (we'll see).

jdkuehne

7:06 pm on Jul 10, 2008 (gmt 0)

10+ Year Member



I thought I could figure out how to pass a link url to Rocknbil's script, but so far, no joy.


<form method="get" action="http://www.example.com" onSubmit="return didYouCheckIt();">

how can I pass the url passthrough.html on the referring page, for example,


<a href="validate.html?passthrough.html">go here</a>

to the action attribute in the form?

rocknbil

4:19 pm on Jul 11, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, you can do this using Javascript but there are two reasons I wouldn't. Primarily, the whole thing falls down if Javascript is disabled. Second, it is a bit more complex because you have to pass the query string to a second page, and parse it out. You'd probably want to set a cookie and re-read it. This is better managed by a server-side program in PHP or perl.

A discussion of this approach is here [webmasterworld.com] including a complete sample script to do the job.

Basically all you'd do is put this Javascript on the "exit page." Come to think of it, if you redirect them to the "exit page", in effect you probably wouldn't need the check box.

jdkuehne

7:45 pm on Jul 11, 2008 (gmt 0)

10+ Year Member



Thanks Rocknbil. The boss has agreed to let me install Perl on our Win2000 IIS server so I can set up a server side solution. I was just hoping for an easier way, plus I have no experience installing Perl, so I'm not sure what I'm about to get into...

rocknbil

8:49 pm on Jul 11, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That script needs no modules, does not do email, and should run as well on Windblows servers as well as any other. Just make sure the directory it's in can execute, or set the file itself to execute, and it should work fine.

Fotiman

8:50 pm on Jul 11, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Perl? Why not go for a more modern server side language, like PHP or ASP.NET? Just curious.

jdkuehne

9:26 pm on Jul 11, 2008 (gmt 0)

10+ Year Member



Why not go for a more modern server side language, like PHP or ASP.NET?

Well, no good reason, I guess except, as a cut and paster, I've had some experience working with Perl scripts, plus the script Rocknbil referred me to is a Perl script. I just don't know enough to write code from scratch (I was a communications major 30 years ago - no programming ability at all! ;)