Adding a checkbox to agree to T & C's

Discussion in 'PHP' started by chrisj, Jul 5, 2007.

  1. #1
    How can I add a checkbox that's required to be checked, before this form info is submitted, with text saying You Must Agree To The Terms & Conditions, or this form can't be submitted unless the box is checked?

    And a link from the words Terms & Conditions. thanks....

    This is the page I want to add it to the bottom of:


    <?php 	global $account;	?>
    
    <script language="javascript" type="text/javascript"> function populate(frm){     frm.email.value = frm.user_name.value } </script> 
    
    <form method="POST" action="index.php"> 
    
    <TABLE align='center' width="440" valign='top' cellpadding="0" cellspacing="1" style='border:1px solid gray' style="padding: 5px;" >
    <tr><td>&nbsp;</td></tr> 
    
    <input class='field' type='hidden' name='command' value='account'> <input class='field' type='hidden' name='param' value='signup'> <tr><td colspan='2' class='pageTitle'><div class='hLine'><font face="Arial" color="#000000" size="3">Create New Account</font></div></td></tr> <tr><td>&nbsp;</td></tr> <tr> <td class='fieldLabel' width="30%"><font face="Arial" color="#6B6B6B" size="2">Membership Type</font></td> 
    
    <td align="center">[account_type_panel]</td></tr> <tr><td>&nbsp;</td></tr> <tr> <td class='fieldLabel' width="30%"><font face="Arial" color="#6B6B6B" size="2">&nbsp;User Name</font></td> <td><input class='field' type='text' size='30' onBlur="populate(this.form);" maxlength="30" name='user_name' value='<?=$account->user_name ?>'> <span class='requiredField'>&nbsp;*&nbsp;</span><span class='errorMessage'><?=$account->get_error('user_name') ?></span></td> </tr> 
    
    <tr> <td class='fieldLabel'><font face="Arial" color="#6B6B6B" size="2">&nbsp;Password</font></td> <td><input class='field' type='password' size='30' maxlength="30" name='password' value='<?=$account->password ?>'> <span class='requiredField'>&nbsp;*&nbsp;</span><span class='errorMessage'><?=$account->get_error('password') ?></span></td> </tr>
    
    <tr> <td class='fieldLabel'><font face="Arial" color="#6B6B6B" size="2">&nbsp;Re-Enter Password</font></td> <td><input class='field' type='password' size='30' maxlength="30" name='reenter_password' value='<?=$account->reenter_password ?>'> <span class='requiredField'>&nbsp;*&nbsp;</span><span class='errorMessage'><?=$account->get_error('reenter_password') ?></span></td> </tr> 
    
    <tr> <td class='fieldLabel'><font face="Arial" color="#6B6B6B" size="2">&nbsp;First Name</font> </td> <td ><input class='field' type='text' size='40' maxlength="50" name='first_name' value='<?=$account->first_name ?>'> <span class='requiredField'>&nbsp;*&nbsp</span><span class='errorMessage'><?=$account->get_error('first_name') ?></span></td>  </tr>
     
    <tr> <td class='fieldLabel'><font face="Arial" color="#6B6B6B" size="2">&nbsp;Last Name</font></td> <td><input class='field' type='text' size='40' maxlength="50" name='last_name' value='<?=$account->last_name ?>'> <span class='requiredField'>&nbsp;*&nbsp;</span><span class='errorMessage'><?=$account->get_error('last_name') ?></span></td> 
    
    </tr> <tr><td></td></tr><tr><td class='fieldLabel'><font face="Arial" color="#6B6B6B" size="2">&nbsp;Email Address</font></td> <td><input class='field' type='text' size='40' maxlength="50" name='email' value='<?=$account->email ?>'> <span class='requiredField'>&nbsp;*&nbsp;</span><span class='errorMessage'><?=$account->get_error('email') ?></span></td> </tr> 
    
    <tr> <td class='fieldLabel'><font face="Arial" color="#6B6B6B" size="2">&nbsp;Address</font></td> <td><input class='field' type='text' size='40' maxlength="70" name='address' value='<?=$account->address ?>'> <span class='requiredField'>&nbsp;*&nbsp;</span><span class='errorMessage'><?=$account->get_error('address') ?></span></td> </tr> 
    
    <tr> <td class='fieldLabel'><font face="Arial" color="#6B6B6B" size="2">&nbsp;Address 2</font></td> <td><input class='field' type='text' size='40' maxlength="70" name='address_second' value='<?=$account->address_second ?>'></td>  </tr> 
    
    <tr> <td class='fieldLabel'><font face="Arial" color="#6B6B6B" size="2">&nbsp;City</font></td> <td><input class='field' type='text' size='40' maxlength="50" name='city' value='<?=$account->city ?>'> <span class='requiredField'>&nbsp;*&nbsp;</span><span class='errorMessage'><?=$account->get_error('city') ?></span></td> </tr>  
    
    <tr> <td class='fieldLabel'><font face="Arial" color="#6B6B6B" size="2">&nbsp;State/Province</font></td> <td><input class='field' type='text' size='35' maxlength="35" name='phone' value='<?=$account->phone ?>'>&nbsp;*&nbsp;</td> </tr>
    
    <tr> <td class='fieldLabel'><font face="Arial" color="#6B6B6B" size="2">&nbsp;Zip/Postal Code</font></td> <td><input class='field' type='text' size='35' maxlength="35" name='zip_postal_code' value='<?=$account->zip_code ?>'> <span class='requiredField'>&nbsp;*&nbsp; </span><span class='errorMessage'><?=$account->get_error('zip_code') ?></span></td> </tr>
    
    <tr> <td class='fieldLabel'><font face="Arial" color="#6B6B6B" size="2">&nbsp;Country</font></td> <td><input class='field' type='text' size='35' maxlength="35" name='mobile_phone_number' value='<?=$account->mobile_phone_number ?>'>&nbsp;*&nbsp;</td></tr>
    
    <tr><td>&nbsp;</td></tr><tr><td colspan="2" class='hLine' align="right"><span class='requiredField'>&nbsp;*</span><font face="Arial" color="#000000" size="2">&nbsp;&nbsp; Required Field</font></td></tr> <tr><td colspan='2' align="right"><input class='button' type='submit' value='create'></td></tr><tr><td colspan="2">&nbsp;</td></tr> </table> </form>
    Code (markup):

     
    chrisj, Jul 5, 2007 IP
  2. JOGS_DEV

    JOGS_DEV Peon

    Messages:
    136
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I use this script to validate whether user has checked the checkbox under Terms and Conditions.

    <script language="JavaScript" type="text/javascript">
    function validator(){
    	var obj = document.formname;
    	if (obj.agreement.checked != true)
    		{
    		alert("You must read and accept Terms and Conditions");
    		obj.agreement.focus();
    		return false;
    		}
    	return true;
    	}
    </script>
    Code (markup):
    You need to substitute the formname in var obj = document.formname with the name of your form. This is a source for the form:
    <form name="formname" method="POST" onsubmit="return validator();" action="submit.php">
    Code (markup):
    This is a source for checkbox:
    <input type="checkbox" name="agreement" value="yes" /> I have read and I accept Terms and Conditions
    Code (markup):
    Good luck!
     
    JOGS_DEV, Jul 6, 2007 IP
  3. infernaliuns

    infernaliuns Active Member

    Messages:
    121
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    55
    #3
    infernaliuns, Jul 6, 2007 IP
  4. ecentricNick

    ecentricNick Peon

    Messages:
    351
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Note however, this assumes the user has javascript enabled. Rather obvious, I know, but seeing as you posted in the PHP forum, you'll still need something server side, else those without javascript will be able to submit without clicking your box and hence legally won't have agreed to your T&Cs.
     
    ecentricNick, Jul 6, 2007 IP
  5. Cloudberries

    Cloudberries Peon

    Messages:
    74
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Good solution! Would you also need some code on the serverside though, to check the result of the variable, as some people may have Javascript disabled, and so the validation on the user's side wouldn't work?

    Edit: or, yeah, what he ^ said ;)
     
    Cloudberries, Jul 6, 2007 IP
  6. ecentricNick

    ecentricNick Peon

    Messages:
    351
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #6
    LOL CLoudberries, we both posted at the same time with the same warning!
     
    ecentricNick, Jul 6, 2007 IP
  7. chrisj

    chrisj Well-Known Member

    Messages:
    606
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #7
    Is there any way to accomplish my request without a javascript, so any user can the user must agree before Submitting?
     
    chrisj, Jul 6, 2007 IP
  8. ecentricNick

    ecentricNick Peon

    Messages:
    351
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #8
    No, because that would mean it's done client side and the user *always* has the option of turning any form of client side scripting off.

    But all you have to do is test the value when they do submit and if it isn't checked, send them back to the form (though do make sure you pre-populate it the second time with everything they typed first time!)

    It's really not difficult to do (off top of my head)...

    <?php
    $valid = false;

    if ($_POST['termsAndConditions']==1){
    $valid=true;
    }

    if (!$valid){
    echo '<form action="address of this page"........ method=..............>';
    echo '<input type="text" name="something" value=$_POST["something"]>';
    echo 'Agree Terms and Conditions<input type="checkbox" name="termsAndConditions">';
    }else{
    // We're all good to go, redirect to the next page...
    header("302.............
    }
     
    ecentricNick, Jul 6, 2007 IP