Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Apr 2004
    Posts
    80
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How to i put a tick box that is required to be ticked?

    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

  • #2
    Regular Coder
    Join Date
    Jan 2005
    Posts
    153
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    PHP Code:
    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!

  • #3
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,172
    Thanks
    19
    Thanked 65 Times in 64 Posts
    if a checkbox is not ticked then it is not passed with the form data so you could just do

    PHP Code:
    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

    Code:
    function checkTerms(obj){
    if(!obj.ofAge.checked){
    alert('You must agree to the terms');
    return false;
    }
    return true;
    }
    
    <form onsubmit="return checkTerms(this)">
    Last edited by NancyJ; 09-16-2005 at 12:59 PM.

  • #4
    Regular Coder
    Join Date
    Jan 2005
    Posts
    153
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That works too... but if you don't want to put all the script stuff in the if..else.. statement, you can do this:
    PHP Code:
    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)
    Code:
    <?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?
    Last edited by Yaggles; 09-16-2005 at 01:04 PM.

  • #5
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,172
    Thanks
    19
    Thanked 65 Times in 64 Posts
    Quote Originally Posted by Yaggles
    That works too... but if you don't want to put all the script stuff in the if..else.. statement, you can do this:
    PHP Code:
    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

  • #6
    New Coder
    Join Date
    Jul 2005
    Location
    Perth, Australia. Age: 13
    Posts
    81
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You could die it putting a back link :P
    I lost my signature, have you seen it?
    I have the power to erase you...

  • #7
    New Coder
    Join Date
    Apr 2004
    Posts
    80
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:
    Code:
    <?
    $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>

  • #8
    New Coder
    Join Date
    Jul 2005
    Location
    Perth, Australia. Age: 13
    Posts
    81
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
    <?
    $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.";
    }
    ?>
    I lost my signature, have you seen it?
    I have the power to erase you...

  • #9
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,172
    Thanks
    19
    Thanked 65 Times in 64 Posts
    You're sending the mail before you check the age

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

  • #10
    New Coder
    Join Date
    Jul 2005
    Location
    Perth, Australia. Age: 13
    Posts
    81
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well, at least you said what was wrong.
    I lost my signature, have you seen it?
    I have the power to erase you...


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •