SitePoint Sponsor

User Tag List

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

    Help with Contact Form checkbox

    I was provided this code for a Contact Form where the Agree To Terms have to checked in a checkbox in order to submit.
    However, I tested and can proceed without checking the box.
    Can you help me figure out how to make it mandatory to check the box in order to submit?

    Thanks

    Code:
    <?php
    $mailto     = 'email@email.com';
    $mailsubj   = "Contact Form submission";$mailhead   = "From:CForm\n";
    $mailbody   = "--- Contact form results ---\n";
    foreach($_REQUEST as $key => $value)    
    {   
     if($key != 'PHPSESSID')        
    {        
    $mailbody .= $key.": ".$value."\n";        
    }    
    }
    $continue = true;if(isset($_POST['ans']) && $_POST['ans']!='hot')
    {  
    echo 'Wrong answer!';  
    $continue = false;
    }
    // if the check box is not checked it will not appear in the $_POST values, it's better to use isset rather than empty
    if(isset($_POST['agree']))
    {  
    echo "If you agree with the terms, check the Agree check box";  
    $continue = false;
    }
    if($continue)
    {  
    $mailbody .= date('Y-m-d H:i:s',strtotime("now"));  
    mail($mailto, $mailsubj, $mailbody, $mailhead); 
    echo "<h2>Thanks!</h2>";  
    //print_r($_REQUEST);
    }
    ?>

  2. #2
    SitePoint Addict goldensona's Avatar
    Join Date
    Jun 2011
    Posts
    226
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi

    use javascript validation for checkbox is checked or not. if checked means show alert message else allow to submit form.

  3. #3
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This part is the wrong way around. It's preventing the form from submitting IF the checkbox was checked.
    PHP Code:
    if(isset($_POST['agree']))
    {  
    echo 
    "If you agree with the terms, check the Agree check box";  
    $continue false;

    That should be if(!isset()) to stop the submission when not checked.

    Also:
    PHP Code:
    $mailbody .= date('Y-m-d H:i:s',strtotime("now"));  //strtotime not necessary
    $mailbody .= date('Y-m-d H:i:s'); //same thing 


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
  •