SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2008
    Posts
    188
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Newbie Confounded by Script Conflict

    On http://porscherepairfountainvalley.com/contact.php I originally started out with no captcha in place. Once the form was filled out it would be sent by emailform.php and everything worked fine. Later on they asked for captcha to be added so I looked around and found jQuery QapTcha. I installed that (the PHP part of it is in contact.php) but even though it left action="emailform.php" completely intact on the FORM element it undid emailform.php being able to actually send the form submission as an e-mail. I tried different ways of blending the two scripts together but to no avail. Anyone have any suggestions?

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Make a very simple form with one element and the captcha.

    Make a very simple formhandler which does var_dump($_POST).

    Discover what PHP supposed to detect on the backend in order to have the captcha pass.

    See if you can make that work first -- learn, then apply it to your more complicated form.

    If you get stuck then post the shortest amount of code which encapsulates your problem here, on this forum and someone might be able to help you.

    Just saying "its broke now" is unlikely to get you any quality replies.

  3. #3
    SitePoint Zealot
    Join Date
    Jul 2008
    Posts
    188
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You are right. In this case I wanted to see if somebody would know on principle what would cause such a conflict just from the general knowledge I gave.

    The following is the initial HTML for the form (taken from contact.php):

    Code:
    <form id="testForm" action="emailform.php" method="post">
    
    	<fieldset>
    		
    		<label for="name">Name</label>
    		<input type="text" id="name" name="name" required="required" tabindex="1" />
    		
    		<label for="email">E-mail</label>
    		<input type="email" id="email" name="email" tabindex="2" />
    		
    		<label for="phone">Phone #</label>
    		<input type="tel" id="phone" name="phone" tabindex="3" />
    		
    		<label for="datetime">Date</label>
            
            <p>If you are making an appointment fill in the date and time below that you would like to come in and we will get back to you to confirm:</p>
            
    		<input type="datetime" id="datetime" name="datetime" tabindex="4" placeholder="Date/Time" />
    		
    		<label for="message">Questions/Comments</label>
    		<textarea id="message" name="message" required="required" tabindex="5"></textarea>
    			
    		<input type="submit" id="testButton" tabindex="6" value="Submit" />
    
    	</fieldset>
    
    	<p id="thanks">Thanks for your submission! We'll be in touch with you shortly.</p>
    	
    </form>
    Next up is the code for emailform.php which has remained untouched throughout the duration of the project (except for e-mail addresses being changed so that the real ones won't be shown here):

    PHP Code:
    <?php

    $page_title 
    'Contact Us Form Submission';

    include (
    'header.php');

    //This is a very simple PHP script that outputs the name of each bit of information (that corresponds to the <code>name</code> attribute for that field) along with the value that was sent with it right in the browser window, and then sends it all to an email address (once you've added it to the script).

    if (empty($_POST)) {
        print 
    "<p>No data was submitted.</p>";
        print 
    "</body></html>";
        exit();
    }

    //Creates function that removes magic escaping, if it's been applied, from values and then removes extra newlines and returns to foil spammers. Thanks Larry Ullman!
    function clear_user_input($value) {
        if (
    get_magic_quotes_gpc()) $value=stripslashes($value);
        
    $valuestr_replace"\n"''trim($value));
        
    $valuestr_replace"\r"''$value);
        return 
    $value;
        }


    if (
    $_POST['comments'] == 'Please share any comments you have here'$_POST['comments'] = '';    

    //Create body of message by cleaning each field and then appending each name and value to it

    $body ="Here is the data that was submitted:\n";

    foreach (
    $_POST as $key => $value) {
        
    $key clear_user_input($key);
        
    $value clear_user_input($value);
        if (
    $key=='extras') {
            
        if (
    is_array($_POST['extras']) ){
            
    $body .= "$key: ";
            
    $counter =1;
            foreach (
    $_POST['extras'] as $value) {
                    
    //Add comma and space until last element
                    
    if (sizeof($_POST['extras']) == $counter) {
                        
    $body .= "$value\n";
                        break;}
                    else {
                        
    $body .= "$value, ";
                        
    $counter += 1;
                        }
                    }
            } else {
            
    $body .= "$key$value\n";
            }
        } else {

        
    $body .= "$key$value\n";
        }
    }

    extract($_POST);
    //removes newlines and returns from $email and $name so they can't smuggle extra email addresses for spammers
    $email clear_user_input($email);
    $name clear_user_input($name);

    //Create header that puts email in From box along with name in parentheses and sends bcc to alternate address
    $from='From: '$email "(" $name ")" "\r\n" 'Bcc: info@gmail.com' "\r\n";


    //Creates intelligible subject line that also shows me where it came from
    $subject 'Porsche Repair Fountain Valley Contact Us Submission';
    $body preg_replace("/^(?=\n)|[^\r](?=\n)/""\\0\r"$body);


    mail ('info@gmail.com'$subject$body$from); }

    ?>

    <p>Thanks for your submission! We'll be in touch with you shortly.</p>
                      
    <?php

    include ('footer.php');

    ?>

    Next up is what the code for the form in contact.php looks like now with the QapTcha PHP added above the HTML and one little QapTcha DIV added near the end of the form:

    Code:
    <?php
    	// if form is submit
    	if(isset($_POST['submit']))
    	{
    		$response = '<div class="notice">';
    		
    		if(isset($_POST['iQapTcha']) && empty($_POST['iQapTcha']) && isset($_SESSION['iQaptcha']) && $_SESSION['iQaptcha'])
    		{
    			$response .= 'Form can be submitted';
    			unset($_SESSION['iQaptcha']);
    		}
    		else
    			$response .= 'Form can not be submitted';
    			
    		$response .= '</div>';
    		
    		echo $response;
    	}
    ?>
    
    <form id="testForm" action="emailform.php" method="post">
    
    	<fieldset>
    		
    		<label for="name">Name</label>
    		<input type="text" id="name" name="name" required="required" tabindex="1" />
    		
    		<label for="email">E-mail</label>
    		<input type="email" id="email" name="email" tabindex="2" />
    		
    		<label for="phone">Phone #</label>
    		<input type="tel" id="phone" name="phone" tabindex="3" />
    		
    		<label for="datetime">Date</label>
            
            <p>If you are making an appointment fill in the date and time below that you would like to come in and we will get back to you to confirm:</p>
            
    		<input type="datetime" id="datetime" name="datetime" tabindex="4" placeholder="Date/Time" />
    		
    		<label for="message">Questions/Comments</label>
    		<textarea id="message" name="message" required="required" tabindex="5"></textarea>
    
    		<div class="QapTcha"></div>
    			
    		<input type="submit" id="testButton" tabindex="6" value="Submit" />
    
    	</fieldset>
    
    	<p id="thanks">Thanks for your submission! We'll be in touch with you shortly.</p>
    	
    </form>
    Next is Qaptcha.jquery.php which is its own PHP file that works with the PHP code added into contact.php

    PHP Code:
    <?php
    session_start
    ();

    $aResponse['error'] = false;
    $_SESSION['iQaptcha'] = false;    
        
    if(isset(
    $_POST['action']))
    {
        if(
    htmlentities($_POST['action'], ENT_QUOTES'UTF-8') == 'qaptcha')
        {
            
    $_SESSION['iQaptcha'] = true;
            if(
    $_SESSION['iQaptcha'])
                echo 
    json_encode($aResponse);
            else
            {
                
    $aResponse['error'] = true;
                echo 
    json_encode($aResponse);
            }
        }
        else
        {
            
    $aResponse['error'] = true;
            echo 
    json_encode($aResponse);
        }
    }
    else
    {
        
    $aResponse['error'] = true;
        echo 
    json_encode($aResponse);
    }
    There is also some jQuery that goes along with the QapTcha script but I will only post it if somebody deems it necessary to solving this problem. The point is that the contents of emailform.php never changed in this process and since it remains the value of the "action" attribute on the FORM element I'm not sure why adding additional PHP would deactivate emailform.php's ability to send the contents of the form submission. Do I maybe need to blend Qaptcha.jquery.php and emailform.php somehow?


Tags for this Thread

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
  •