Welcome to WebmasterWorld Guest from 18.204.48.199

Forum Moderators: open

Message Too Old, No Replies

Need help with a JavaScript checkbox validator

Needs to pass URL through

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

Junior Member

10+ Year Member

joined:Jan 11, 2005
posts: 76
votes: 0


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.

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

Administrator

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 31, 2003
posts:12555
votes: 3


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
}
}
2:35 pm on July 9, 2008 (gmt 0)

Senior Member

joined:Nov 12, 2005
posts:5967
votes: 0


Just note there is an extra closing bracket in there at the end that should be removed :)
5:54 pm on July 9, 2008 (gmt 0)

Senior Member

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 28, 2004
posts:7999
votes: 0


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>
8:17 pm on July 9, 2008 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 11, 2005
posts: 76
votes: 0


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).

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

Junior Member

10+ Year Member

joined:Jan 11, 2005
posts: 76
votes: 0


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?

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

Senior Member

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 28, 2004
posts:7999
votes: 0


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.

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

Junior Member

10+ Year Member

joined:Jan 11, 2005
posts:76
votes: 0


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...
8:49 pm on July 11, 2008 (gmt 0)

Senior Member

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 28, 2004
posts:7999
votes: 0


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.
8:50 pm on July 11, 2008 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Oct 17, 2005
posts:5021
votes: 26


Perl? Why not go for a more modern server side language, like PHP or ASP.NET? Just curious.
9:26 pm on July 11, 2008 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 11, 2005
posts: 76
votes: 0


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! ;)