Contact Form tweaks

Discussion in 'PHP' started by chrisj, Aug 15, 2011.

  1. #1
    Someone had helped me set up a basic Contact Form that simply sends the info to an email address upon submission.
    Additionally, I got help to add the functionality of a CAPTCHA, and a function to Agree To Terms.
    Well, instead of Captcha, a simple question is posed in the Form: Is Fire Hot or Cold?

    Anyway, I plugged in the suggested lines of code into the html form, and the accompanying very simple php handler (below).

    After answering the question (wrong), the user exits to a blank page that simply says "wrong answer". I'd like to have the words "Wrong Answer" appear on the Contact Form page, instead of exiting.

    And I'd like to add a link to the Agree To Terms, so the user can read the terms they are agreeing to, and also not allow a user to submit/proceed until he agrees.

    I'm sure these are very simple tweaks. Please let me know if you're interested in helping me "have the words "Wrong Answer" appear on the Contact Form page, instead of exiting" and "add a link to the Agree To Terms, so the user can read the terms they are agreeing to, etc.".

    Thanks



    <?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";
    		}
    	}
    if(isset($_POST['ans']) && $_POST['ans']!='hot') exit('Wrong answer');
    
    if(empty($_POST['agree']) || $_POST['agree'] != 'agree')
    {
    echo "If you agree with the terms, check the Agree check box";
    exit;
    }
    
    $mailbody .= date('Y-m-d H:i:s',strtotime("now"));
    mail($mailto, $mailsubj, $mailbody, $mailhead);
    echo "<h2>Thanks!</h2>"
    //print_r($_REQUEST);
    ?>
    Code (markup):
     
    Last edited: Aug 15, 2011
    chrisj, Aug 15, 2011 IP
  2. JohnnySchultz

    JohnnySchultz Peon

    Messages:
    277
    Likes Received:
    4
    Best Answers:
    7
    Trophy Points:
    0
    #2
    try this..

    <?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);
    }
    ?>
    PHP:
     
    JohnnySchultz, Aug 16, 2011 IP
  3. Technoslab

    Technoslab Peon

    Messages:
    46
    Likes Received:
    3
    Best Answers:
    3
    Trophy Points:
    0
    #3
    I suggest you to add some strong capcha mechanism to avoid getting your inbox flooded.
     
    Technoslab, Aug 16, 2011 IP
  4. chrisj

    chrisj Well-Known Member

    Messages:
    606
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #4
    I have resolved the error
     
    Last edited: Aug 17, 2011
    chrisj, Aug 17, 2011 IP
  5. chrisj

    chrisj Well-Known Member

    Messages:
    606
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #5
    Thanks for the code, however, upon testing, I am able to proceed/submit even if the Agree To Terms checkbox is unchecked.
    Can you help me remedy this?
    I'd like it so the user is unable to proceed unless the heckbox is checked. Thanks again
     
    chrisj, Aug 17, 2011 IP
  6. billu1995

    billu1995 Guest

    Messages:
    159
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    should make a class to perform this
     
    billu1995, Aug 17, 2011 IP
  7. chrisj

    chrisj Well-Known Member

    Messages:
    606
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #7
    Thanks for your help
     
    Last edited: Aug 18, 2011
    chrisj, Aug 18, 2011 IP