SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    calif
    Posts
    743
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to add a required Agree To These Terms line into an existing Form ?

    I understand that the code below is not state-of-the-art coding, but I'd rather just add a line and not rewrite the whole Form, please.

    I'd like to add a required line that basically states "Read These Terms And Click Here To Agree To The Terms" so that the user can't submit the Form info without completing that line. Thanks for any help.

    Code:
    <table cellpadding="0" cellspacing="0" align="center" width="600">
    <tr>
      <td>
        <font face="verdana"><font size="3"><font color=#800000;">[var.result]</font></font><font color="red"><strong>[var.error]</strong></font><br>
        <hr size="1" />
        <br />
    
        <form action="/page.php?page=1" method="post" name="contact_us" onSubmit="return capCheck(this);">
    
        <table cellpadding="5" width="100%">
        <tr>
    		  <td width="10" class="required_field">*</td>
          <td width="80">Your Name</td>
          <td>&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" name="name" maxlength="50" style="width:300px; border: 1px solid #696969;" /><br/><br/></td>
        </tr>
        <tr>
      <td class="required_field">*</td>
      <td>Email Address</td>
          <td>&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" name="email" maxlength="40" style="width:300px; border: 1px solid #696969;" /><br/><br/></td>
        </tr>
        <tr>
        <td></td>
        
      <td>Subject:</td>
          <td>&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" name="subject" maxlength="40" style="width:300px; border: 1px solid #696969;"/><br/><br/></td>
        </tr>
            <tr>
            <td></td>
          <td>Comments:</td>
          <td>&nbsp;&nbsp;&nbsp;&nbsp;<textarea name="comments" style="width: 100%; height: 250px; background: #F5F5F5; border: 1px solid #F5F5F5;"></textarea><br/><br/></td>
        </tr>
    <tr>
          <td class="required_field">*</td>
          <td>Enter Image Code:</td>
          <td>&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" value="" name="captext" style="width: 100px" maxlength="6" /></td>
        </tr>
    <tr>
          <td></td>
          <td><a onclick="refresh_security_image(); return false;" style="cursor:pointer;"><u>Refresh Image</u></a></td>
          <td>&nbsp;&nbsp;&nbsp;&nbsp;<img src="includes/captcha.php" border="0" id="verificiation_image" /></a></td>
        </tr>
        </table>
    <br/>
    
    <br/>
        <p>
          <input type="hidden" name="submited" value="1" />
          &nbsp;&nbsp;&nbsp;&nbsp;<input type="submit" name="submit" value="&nbsp;&nbsp;&nbsp;&nbsp;Submit&nbsp;&nbsp;&nbsp;&nbsp;" style="margin:7px 10px 0px 0px; padding:10px 0px 10px 0px; font-size:15px; font-style:Century-Gothic;" />
        </p>
    
        </form>
    
      </td>
    </tr>
    </table>

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,941
    Mentioned
    215 Post(s)
    Tagged
    12 Thread(s)
    Good morning,

    This is one possible solution.

    Add the checkbox to your form:

    HTML Code:
    <tr>
      <td class="required_field">&nbsp;</td>
      <td colspan="2"><input type="checkbox" name="acceptTerms" /> I have read and accepted the terms</td>
    </tr>
    Validate the result:

    Your form is currently using the return value of the JavaScript function capCheck() to check if the captcha field was filled out correctly.
    You could also validate whether the check box was checked within the same function.

    Code JavaScript:
    function capCheck(f){
      ...
      if (f.acceptTerms.checked == 'false'){
        alert("Please accept the terms and conditions);
        return false
      }
      ...
    }

    I hope that helps.
    If you have any questions, just let me know.

  3. #3
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    calif
    Posts
    743
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for that reply. Much appreciated. It's very helpful.

    I just don't know how to integrate this Code:
    Code:
    function capCheck(f){
      ...
      if (f.acceptTerms.checked == 'false'){
        alert("Please accept the terms and conditions);
        return false
      }
      ...
    }

    Into my existing Code:




    Code:
    								if($_POST['submited'] == "1"){
    
    
    							$your_email = "support@somemiscxsite.com";  //This is your email address -
    							$from = "From: Message-form@somemisxcsite.com". "\r\n"; //This will show as the email sender. -
    						    $user_email = $_POST['email'];
    							$user_name = $_POST['name'];
    							$subject = $_POST['subject'];
    							$email_body = $_POST['comments'];
    
    							 if($user_email == "" or $user_name == "" or $subject == "" or $email_body == ""){
    							 $error = "Please Complete All Required* Fields";
    							}else{
    
    							$message = "ContactUs : \n \nUsers Email : $user_email \nUsers Name : $user_name  \n\n\n Users Message : \n\n$email_body";
    
    
    						 //SEND THE EMAIL -
    
    							mail($your_email, $subject, $message, $from);
    
    						    $result = "Your Message Has Been Sent. Thank You";
    
    							}
    				}
    
    				            $message = "ContactUs : \n \nUsers Email : $user_email \nUsers Name : $user_name \n\n\n Users Message : \n\n$email_body";
    Any help/guidance will be appreciated.

  4. #4
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,941
    Mentioned
    215 Post(s)
    Tagged
    12 Thread(s)
    Hi there,

    Sorry, I was thinking in JavaScript, this is PHP.

    So, make the checkbox on your HTML page like this:
    PHP Code:
    <tr>
      <
    td class="required_field">&nbsp;</td>
      <
    td colspan="2"><input type="checkbox" name="acceptTerms" value="yes" /> I have read and accepted the terms</td>
    </
    tr
    Then add this to the code you just posted:

    PHP Code:
    $acceptTerms $_POST['acceptTerms']; 
    Then change this:
    PHP Code:
    if($user_email == "" or $user_name == "" or $subject == "" or $email_body == ""){ 
    to this:
    PHP Code:
    if($user_email == "" or $user_name == "" or $subject == "" or $email_body == "" or $acceptTerms != "yes"){ 
    Does that work?

  5. #5
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    calif
    Posts
    743
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, that works great - Thanks so much!
    Would you be interested in telling me how (what code) I can have a box pop-up when the user forgets to check the box, saying somthing like please check the box to proceed? Thanks again.

  6. #6
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,941
    Mentioned
    215 Post(s)
    Tagged
    12 Thread(s)
    Hi there,

    Quote Originally Posted by ChrisjChrisj View Post
    Would you be interested in telling me how (what code) I can have a box pop-up when the user forgets to check the box, saying somthing like please check the box to proceed?
    This is something you would do with JavaScript.

    The way you would do it is this:
    • You would attach an onSubmit event to your form, so that when a user clicks the "Submit" button, a JavaScript function is called.
    • This function would then evaluate whether the box had been checked.
    • If it has, then it would do nothing and the form would be submitted to your PHP script.
    • If it hasn't, then you would display an error message and return a value of "false".
    • Returning "false" would prevent the form from being submitted.
    Here's a simple example for you to play around with:

    HTML Code:
    <!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=utf-8" />
        <title>Form checkbox example</title>
        <style>div{margin-bottom:10px;}</style>
      </head>
      
      <body>
        <form action="myscript.php" method="post" id="myForm">
          <div>
            <label for="accountNo">Bank account number:</label>
            <input type="text" id="accountNo" name="accountNo" />
          </div>
          <div>
            <label for="terms">I accept the terms and conditions.</label>
            <input type="checkbox" id="terms" name="terms" />
          </div>
          
          <input type="submit" value="Submit" />
        </form>
        
        <script>
          f = document.getElementById("myForm");
          f.onsubmit=function(){
            if (f.terms.checked){
              alert("You accepted the terms. The form will now be submitted");
            } else {
              alert("Please accept the terms and conditions first!");
              return false;
            }
          }
        </script>
      </body>
    </html>
    Hopefully that's straightforward enough, but if you have any questions, just let me know.

    Edit:
    In the real world, you probably don't want to do anything if the form is submitted correctly, so you could reverse the above condition to only display a message if the box isn't ticked (note the exclamation mark in the if statement):

    Code JavaScript:
    <script>
      f = document.getElementById("myForm");
      f.onsubmit=function(){
        if (!f.terms.checked){
          alert("Please accept the terms and conditions first!");
          return false;
        }
      }
    </script>


Bookmarks

Posting Permissions

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