...

View Full Version : How to i put a tick box that is required to be ticked?



muggzy
09-16-2005, 01:35 PM
Hi there, ive create a formail script and i want a tick box at the bottom which says 'tick here if you are over 18 and agree to the terms and conditions' and the form can only be submitted if this is ticked

if this is not ticked i'd like a message to appear when someone tries to submit that tells them the need to be over 18 etc.

is this gonna be a tough one?

thanks people

Yaggles
09-16-2005, 01:46 PM
Not hard at all.

First, you need to make sure that checkbox has a "name" attribute assigned to it (<input type="checkbox" name="ofage" value="yes" />). Then, on the page you submit to, you need to check and

if($HTTP_POST_VARS['ofage'] != 'yes')
{
die('You must agree to the terms.');
}
else
{
//the script's code
}

Also, the form's method attribute must be "post".

Got all that? If you have any problems, just say so and I'll try to help!

NancyJ
09-16-2005, 01:55 PM
if a checkbox is not ticked then it is not passed with the form data so you could just do



if(isset($_POST['ofage'])){
//the script's code
}
else
{
echo 'You must agree to the terms.';
}


from a useability point of view you'd be better putting in javascript checking too



function checkTerms(obj){
if(!obj.ofAge.checked){
alert('You must agree to the terms');
return false;
}
return true;
}

<form onsubmit="return checkTerms(this)">

Yaggles
09-16-2005, 01:59 PM
That works too... but if you don't want to put all the script stuff in the if..else.. statement, you can do this:


if(!isset($HTTP_POST_VARS['ofage']))
{
die('You must read and agree to the terms');
}


Then, if it's not set, it stops the whole page content and just shows die() message. If there isn't, it ignores it and does the script.

Either method works. Hope it works for what you need done!
EDIT: Just saw the javascript you edited in. That is also a good idea to use Javascript.
SO this is your script from what we have:
submit_page.php (change to the page you submit to)


<?php
if(!isset($HTTP_POST_VARS['ofage']))
{
die('Please read and agree to the terms before continuing.');
}
//Blah blah blah etc.
?>
<html>
<head>
<script type="text/javascript">
function checkTerms(obj){
if(!obj.ofAge.checked){
alert('You must agree to the terms');
return false;
}
return true;
}
</script>
</head>
<body>
<form onsubmit="return checkTerms(this);">
<!-- Blah Blah form elements, etc. -->
<input type="checkbox" name="ofage" value="yes" />
<!-- Blah Blah more element/submit buttons -->
</form>
</body>
</html>

Got it muggzy?

NancyJ
09-16-2005, 02:03 PM
That works too... but if you don't want to put all the script stuff in the if..else.. statement, you can do this:


if(!isset($HTTP_POST_VARS['ofage']))
{
die('You must read and agree to the terms');
}


Then, if it's not set, it stops the whole page content and just shows die() message. If there isn't, it ignores it and does the script.

Either method works. Hope it works for what you need done!

fwiw if you die() the form wont be displayed for the user to aggree to the terms ;)
Also just fyi $HTTP_POST_VARS is depreciated as of PHP 4.1.0. so unless you're on an old version of php you should use $_POST instead

e-Raser
09-16-2005, 02:28 PM
You could die it putting a back link :P

muggzy
09-16-2005, 06:04 PM
Thanks for the heaps of help, im obviously going wrong somewhere tho as the form just sends with no tick in the checkbox PHP IS:

<?
$name=$_POST['name'];
$email=$_POST['email'];
$comments=$_POST['comments'];
$mobilenumber = $_POST['mobilenumber'];
$mobilehandset =$_POST['mobilehandset'];
$to="murat.m@smartfusion.co.uk";
$message="$name just filled in your Cloud Wifi Event entry form Company Name:\n$comments\n\\nand Their e-mail address was: $email, Mobile number: $mobilenumber Mobile Handset: $mobilehandset";
if(mail($to,"Cloud Wifi Competition Entry!",$message,"From: $email\n")) {
header( "Location: http://www.mintedmango.com/thankyou.html" );
} else {
echo "There was a problem sending the mail. Please check that you filled in the form correctly.";
}
if(!isset($POST['ofage']))
{
die('You must read and agree to the terms');
}
?>


HTML IS
<form action="mail.php" method="post" class="style19">
<div align="left">
<p><span class="style19">Your Name:
<input type="text" name="name">
<br>
E-mail:
<input type="text" name = "email">
</span></p>
<p>Mobile Number:
<input type="text" name = "mobilenumber">
</p>
<p> Mobile Handset:
<input type="text" name = "mobilehandset">
</p>
<p>
<input type="checkbox" name="ofage" value="yes" />
</p>
<p><span class="style19"><br>
Company:</span>
<input name="comments" type="text" value="" size="55">
<br>
<br>
<input type="submit" value="Submit">
</p>
</div>
</form>

e-Raser
09-16-2005, 06:13 PM
<?
$name=$_POST['name'];
$email=$_POST['email'];
$comments=$_POST['comments'];
$mobilenumber = $_POST['mobilenumber'];
$mobilehandset =$_POST['mobilehandset'];
$to="murat.m@smartfusion.co.uk";
$message="$name just filled in your Cloud Wifi Event entry form Company Name:\n$comments\n\\nand Their e-mail address was: $email, Mobile number: $mobilenumber Mobile Handset: $mobilehandset";
if(!isset($_POST['ofage']))
{
die('You must read and agree to the terms');
}
if(mail($to,"Cloud Wifi Competition Entry!",$message,"From: $email\n")) {
header( "Location: http://www.mintedmango.com/thankyou.html" );
} else {
echo "There was a problem sending the mail. Please check that you filled in the form correctly.";
}
?>

NancyJ
09-16-2005, 06:26 PM
You're sending the mail before you check the age ;)

Edit: looks like I got beat to it, got distracted mid post - stupid work. ;)

e-Raser
09-16-2005, 06:34 PM
Well, at least you said what was wrong. :D



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum