making sure a checkbox is clicked

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

making sure a checkbox is clicked

Post by Luke »

I am making a form that posts variables to paypal, and I want to make sure that the customer clicks an "I agree to these terms" button before they submit. Is there an easy javascript or something that will make sure that check box is clicked, and if not, make a little box pop up and bitch at them?
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

OK, i figured out a way to do get the box to pup up, but it still lets the form be posted and now after the box pops up, all the information in the box disappears. How do I get it to stay there?
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script>
function acceptTerms() {
	
	if (document.forms['theForm'].elements['theCheckbox'].checked == false) {
		
		alert('Please check the box to continue.');
		return false;

		
	} else {
		return true;

	}
}
</script>
</head>

<body>



<form action="" method="get" name="theForm" onsubmit="return acceptTerms()"/>

<input name="theCheckbox" type="checkbox" value="1" />

<input name="" type="submit" />

</form>
</body>
</html>
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

You could pop up a confirm() box rather than an alert() box. Confirm will return false if the user clicks 'Cancel', and true if the user clicks 'OK'.

Code: Select all

<script>
<html>
<head>
function doubleCheck()
{
   var myConfirm = confirm('Are you sure you want to confirm this?');
   return(myConfirm);
}
</script>
</head>
<body>
<form method = 'post' action = '$PHP_SELF' onSubmit = "return(doubleCheck());">
...
<input type = "submit">
</form>
</body>
</html>
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply