SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Guru bronze trophy AndrewCooper's Avatar
    Join Date
    Sep 2008
    Location
    Manchester, UK
    Posts
    631
    Mentioned
    10 Post(s)
    Tagged
    0 Thread(s)

    Basic Captcha Sum and Subject Dropdown Menu in PHP Form

    Hey All,

    I have a fairly straight forward PHP contact form that already works, but I'm looking to add two new features to it that I'm fairly unfamiliar with how-to implement and was hoping that the SitePoint community would come to my rescue!

    Feature #1 - There is a drop down menu in the contact form with 4 options to choose from. Whichever option the audience selects will be the subject / title of the E-Mail being sent / received. I'm unsure of what to put inside the option value="" attribute and don't have ANY idea of what needs to go in the PHP script.

    Feature #2 - I have added a "Captcha" field to the form that simply asks the audience what 2 + 2 equates to. If the audience types in either "4" or "four" in the field then the form will execute and the E-Mail will be sent. If the field is empty or contains something other than "4" or "four" then the script will end and the E-Mail will not be sent or executed. I've added the HTML to the form and some PHP to the PHP script that processes the form, but I'm unsure if I've set this up correctly or not and in the right order for processing the script.

    I've added the HTML contact form below:

    HTML Code:
    <form action="processContactForm.php" method="post" id="contact">
    	<div><label for="name">Name:</label><input type="text" name="name"></div>
    	<div><label for="email">E-Mail:</label><input type="text" name="email"></div>
    	<div><label for="subject">Regarding:</label>
    		<select id="subject">
    			<option value="">General / Business Enquiry</option>
      			<option value="">Publisher Partner Support</option>
      			<option value="">Indie Author Support</option>
      			<option value="">Customer Support</option>
      		</select>
    	</div>
    	<div><label for="captchasum">2 + 2 =</label><input type="text" name="captchasum"></div>
    	<div><label for="message">Message:</label><textarea name="message" id="contact-form"></textarea></div>
    	<input type="submit" name="submitBtn" value="Submit" id="submitBtn" />
    </form>
    And the PHP script also:

    PHP Code:
    <?php
    // Pick up the form data and assign it to variables
    $name htmlspecialchars($_POST['name']);
    $email htmlspecialchars($_POST['email']);
    // $subject = ; ?????
    $captchasum htmlspecialchars($_POST['captchasum']);
    $message htmlspecialchars($_POST['message']);

    // Build the email
    $to 'andrew.cooper@mysite.com';
    // $subject = ; ?????
    $messageOutput "You have a new <b>E-Mail from:</b> $name <br/>
                <b>Their E-Mail Address:</b> 
    $email <br/>
                <b>Their message is:</b> 
    $message";
    $headers "From: $email";

    // Validate the E-Mail via the Captcha sum
    if (!empty($captchasum) && !($captchasum == "4" || $captchasum == "four")) {
        echo 
    "Please enter either 4 or four in this field.";
        exit ();
    }

    // Send the mail using the PHP mail() function
    mail($to$subject$messageOutput$headers);

    // Redirect
    header("Location: http://www.mysite.com/");
    ?>
    Any and all help will be appreciated and as always, you have my deepest thanks!

  2. #2
    SitePoint Evangelist
    Join Date
    Aug 2006
    Location
    Nantwich, Cheshire
    Posts
    408
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    If the various email subjects come from a database, you could put the row id in the option value and then retrieve in your php code based on that id. If they do not, then the easiest way would be to duplicate the subject text in the option value.

    HTML Code:
    		<select id="subject" name="subject">
    			<option value="General / Business Enquiry">General / Business Enquiry</option>
      			<option value="Publisher Partner Support">Publisher Partner Support</option>
      			<option value="Indie Author Support">Indie Author Support</option>
      			<option value="Customer Support">Customer Support</option>
      		</select>
    PHP Code:
    $subject $_POST['subject']; 
    Or you could just put an integer value in the option value, then have an array in your php code containing the subject text, and pick it out that way.

    HTML Code:
    		<select id="subject" name="subject">
    			<option value="0">General / Business Enquiry</option>
      			<option value="1">Publisher Partner Support</option>
      			<option value="2">Indie Author Support</option>
      			<option value="3">Customer Support</option>
      		</select>
    PHP Code:
    $subj = array('General / Business Enquiry''Publisher Partner Support''Indie Author Support''Customer Support');
    ...
    $subject $subj($_POST['subject']); 
    http://www.firenza.net - my homage to a car from the 1970s

  3. #3
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Andrew,

    Compare the strings

    PHP Code:
    <?php // Pick up the form data and assign it to variables 
    $name htmlspecialchars($_POST['name']); 
    $email htmlspecialchars($_POST['email']); 
    // $subject = ; ????? 
    $captchasum strtolower(htmlspecialchars($_POST['captchasum'])); 
    $message htmlspecialchars($_POST['message']); 




    if((
    $captchasum == 'four') || ($captchasum == '4')) {


        
    // Build the email 
        
    $to 'andrew.cooper@mysite.com'
        
    // $subject = ; ????? 
        
    $messageOutput "You have a new <b>E-Mail from/b> $name <br/> 
                    <b>Their E-Mail Address/b> 
    $email <br/> 
                    <b>Their message is/b> 
    $message"
        
    $headers "From: $email"
        
        
    // Validate the E-Mail via the Captcha sum 
        
    if (!empty($captchasum) && !($captchasum == "4" || $captchasum == "four")) { 
            echo 
    "Please enter either 4 or four in this field."
            exit (); 
        } 
        
        
    // Send the mail using the PHP mail() function 
        
    mail($to$subject$messageOutput$headers); 
        
        
    // Redirect 
    header("Location: http://www.mysite.com/"); 


    } else {
        
    // do something else.....    
    }
    ?>
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  4. #4
    SitePoint Guru bronze trophy AndrewCooper's Avatar
    Join Date
    Sep 2008
    Location
    Manchester, UK
    Posts
    631
    Mentioned
    10 Post(s)
    Tagged
    0 Thread(s)
    Hey, thank you both for the help and the code snippets! And so fast too...Almost as fast as the internet itself!

    I've amended my code, and whilst the Captcha validation if / else statement works fine and dandy, if the Captcha is filled in correctly, the E-Mail isn't sent.

    Is it something to do with the way the code is ordered or am I missing something out that I just haven't spotted? Could you take another look at the code below please?

    HTML code:
    HTML Code:
    <form action="../contact/processContactForm.php" method="post" id="contact">
    	<div><label for="name">Name:</label><input type="text" name="name"></div>
    	<div><label for="email">E-Mail:</label><input type="text" name="email"></div>
    	<div><label for="subject">Regarding:</label>
    		<select id="subject">
    			<option value="General / Business Enquiry">General / Business Enquiry</option>
      			<option value="Publisher Partner Support">Publisher Partner Support</option>
      			<option value="Indie Author Support">Indie Author Support</option>
      			<option value="Customer Support">Customer Support</option>
      		</select>
    	</div>
    	<div><label for="captchasum">2 + 2 =</label><input type="text" name="captchasum"></div>
    	<div><label for="message">Message:</label><textarea name="message" id="contact-form"></textarea></div>
    	<input type="submit" name="submitBtn" value="Submit" id="submitBtn" />
    </form>
    PHP script:

    PHP Code:
    <?php
    // Pick up the form data and assign it to variables
    $name htmlspecialchars($_POST['name']);
    $email htmlspecialchars($_POST['email']);
    $subject $_POST['subject'];
    $captchasum strtolower(htmlspecialchars($_POST['captchasum'])); 
    $message htmlspecialchars($_POST['message']);

    // Validate the E-Mail via the Captcha sum
    if(($captchasum == 'four') || ($captchasum == '4')) {
        
    // Build the email
        
    $to 'andrew.cooper@mysite.com';
        
    $subject "$subject"
        
    $messageOutput "You have a new <b>E-Mail from:</b> $name <br/>
                        <b>Their E-Mail Address:</b> 
    $email <br/>
                        <b>Their message is:</b> 
    $message";
        
    $headers "From: $email";

        
    // Send the mail using the PHP mail() function
        
    mail($to$subject$messageOutput$headers);

        
    // Redirect
        
    header("Location: http://www.mysite.com/");
    } else {
        
    // Print error message and stop executing the script
        
    echo "Please press the back button and enter either 4 or four in the field marked '2 + 2 =' field.";
        exit ();    
    }
    ?>
    Thanks a lot guys!

  5. #5
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Is the redirect happening?
    Have you checked your junk boxes?

    Take off the redirect temporarily to see if there are any error messages displayed.

    You may also need some more email headers to help the email not get binned.

    Here is a snippet I use:

    PHP Code:
    $to 'me@me.com';
    $subject 'Subject line';

    $headers   = array();
    $headers[] = "MIME-Version: 1.0";
    $headers[] = "Content-type: text/plain; charset=iso-8859-1";
    $headers[] = "From: Me <spike@spikezawesomesite.com>";
    $headers[] = "Subject: {$subject}";
    $headers[] = "X-Mailer: PHP/".phpversion();
    mail($to$subject$emailimplode("\r\n"$headers)); 
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  6. #6
    SitePoint Guru bronze trophy AndrewCooper's Avatar
    Join Date
    Sep 2008
    Location
    Manchester, UK
    Posts
    631
    Mentioned
    10 Post(s)
    Tagged
    0 Thread(s)
    Hey Mike,

    Ok, so I changed the recipient E-Mail from andrew.cooper@mysite.com (an Office 365 E-Mail address) to my personal E-Mail maybemynamewhoknows@live.co.uk (Outlook.com E-Mail) and the E-Mail goes through, but no subject line taken from the select options on the form.

    So, the issue now, is that #1 The subject line isn't working and #2 The form isn't even getting through to my Office 365 E-Mail address, even the junk folder. I'll see if I can hunt down some information about the Office 365 E-Mail issue on the web and in the Office 365 Community site, although the spam filtering is set to the lowest already...So don't know what else I might be able to do on that front, but, could somebody help me with the subject line issue?

    The newly updated code that works fine to be sent to my personal E-Mail works fine, everything...Besides the subject line.

    HTML:

    HTML Code:
    <form action="../contact/processContactForm.php" method="post" id="contact">
    	<div><label for="name">Name:</label><input type="text" name="name"></div>
    	<div><label for="email">E-Mail:</label><input type="text" name="email"></div>
    	<div><label for="subject">Regarding:</label>
    		<select id="subject">
    			<option value="General / Business Enquiry">General / Business Enquiry</option>
      			<option value="Publisher Partner Support">Publisher Partner Support</option>
      			<option value="Indie Author Support">Indie Author Support</option>
      			<option value="Customer Support">Customer Support</option>
      		</select>
    	</div>
    	<div><label for="captchasum">2 + 2 =</label><input type="text" name="captchasum"></div>
    	<div><label for="message">Message:</label><textarea name="message" id="contact-form"></textarea></div>
    	<input type="submit" name="submitBtn" value="Submit" id="submitBtn" />
    </form> 

    PHP:

    PHP Code:
    <?php
    // Pick up the form data and assign it to variables
    $name htmlspecialchars($_POST['name']);
    $email htmlspecialchars($_POST['email']);
    $subject $_POST['subject'];
    $captchasum strtolower(htmlspecialchars($_POST['captchasum'])); 
    $message htmlspecialchars($_POST['message']);

    // Validate the E-Mail via the Captcha sum
    if(($captchasum == 'four') || ($captchasum == '4')) {
        
    // Build the email
        
    $to 'apcooper@live.co.uk';
        
    $subjectLine $subject;
        
    $messageOutput "You have a new E-Mail from: $name
                        Their E-Mail Address: 
    $email
                        Their message is: 
    $message";
        
    $headers   = array();
        
    $headers[] = "MIME-Version: 1.0";
        
    $headers[] = "Content-type: text/plain; charset=iso-8859-1";
        
    $headers[] = "From: $name <$email>";
        
    $headers[] = "X-Mailer: PHP/".phpversion();

        
    // Send the mail using the PHP mail() function
        
    mail($to$subjectLine$messageOutputimplode("\r\n"$headers));

        
    // Redirect
        
    header("Location: http://www.huffier.com/");
    } else {
        
    // Print error message and stop executing the script
        
    echo "Please press the back button and enter either 4 or four in the field marked '2 + 2 ='.";
        exit ();    
    }
    ?>
    When the E-Mail is received in my personal E-Mail inbox (not in the junk folder either) the subject line is "(No Subject)".

    Many thanks for all your help.

  7. #7
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Andrew,
    Your <select> needs a NAME as well as an ID

    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  8. #8
    SitePoint Guru bronze trophy AndrewCooper's Avatar
    Join Date
    Sep 2008
    Location
    Manchester, UK
    Posts
    631
    Mentioned
    10 Post(s)
    Tagged
    0 Thread(s)
    Wow. FML. All of that stress and 3 hours of failed debugging yesterday trying all kinds of different alternatives to get the subject line to work...And I needed to add the name attribute in the select tag.

    What an utter idiot I am.

    Thanks Mike, really appreciate all the help. It works perfectly fine now the subject line

    The form still won't get through to my Office 365 E-Mail, so I need to sort that out with Microsoft / Office 365 support tomorrow, but it's set to send E-Mail to my personal E-Mail so I still get it through at least!

    As always, you guys (especially you Mike!) are my heroes!


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
  •