jcadima
5/3/2017 - 6:36 PM

Salesforce with PHP recaptcha

Salesforce with PHP recaptcha


<?php 
// this goes above the HTML head
if ($_SERVER['REQUEST_METHOD'] == 'POST') { 

	if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response']))  {
	extract($_POST) ;  // lazy post extraction
	    //your site secret key
	    $secret = '6Le8wh8UAAAAABIWg-e4i6HjHof-EJNm9Pw_MeRK';
	    //get verify response data
	    $verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response']);
	    $responseData = json_decode($verifyResponse);
	    if($responseData->success) { 
			//set POST variables
			$url = 'https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8';
			$fields = array(
						'oid'=>urlencode($oid),
						'retURL'=>urlencode($retURL),
						'last_name'=>urlencode($last_name),
						'company'=>urlencode($company),					
						'email'=>urlencode($email),
						'phone'=>urlencode($phone)
					);
	 
			//url-ify the data for the POST
			foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
			rtrim($fields_string,'&');
	 
			//print_r($fields_string);
	 
			//open connection
			$ch = curl_init();
	 
			//set the url, number of POST vars, POST data
			curl_setopt($ch,CURLOPT_URL,$url);
			curl_setopt($ch,CURLOPT_POST,count($fields));
			curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
	 
			//execute post
			$result = curl_exec($ch);
	 
			//close connection
			curl_close($ch);
	        
	        $succMsg = 'Your contact request have submitted successfully.';
	      //  echo $succMsg ;
	       // exit;
	    }
	    // response Failed
	    else { 
	        $errMsg = 'Robot verification failed, please try again.';
	       // echo $errMsg ;
	        //exit;
	    }
	}
	    
	else {
	    $errMsg = 'Please click on the reCAPTCHA box.';
	   // echo $errMsg ;
	    //exit;
	}
	

}	


?>



<!-- Contact Form -->
<form method="POST" id="leadform">
   <input type=hidden name="oid" value="00DA0000000HLYJ">
   <input type=hidden name="retURL" value="http://www.freemobilecardreader.com/thankyou.html">
   <input type=hidden name="Referred_By__c" value="SEO - BetterThanSquare">
   <!--  ----------------------------------------------------------------------  -->
   <!--  NOTE: These fields are optional debugging elements. Please uncomment    -->
   <!--  these lines if you wish to test in debug mode.                          -->
   <!--  <input type="hidden" name="debug" value=1>                              -->
   <!--  <input type="hidden" name="debugEmail"                                  -->
   <!--  value="taronica@skybankfinancial.com">                                  -->
   <!--  ----------------------------------------------------------------------  -->
   <p><label for="last_name" class="form_label">Contact Name</label><br/>
      <input  id="last_name" maxlength="80" name="last_name" class="form_input" type="text" value="" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;"/>
   </p>
   <p><label for="company" class="form_label">Company Name</label><br/>
      <input  id="company" maxlength="40" name="company" class="form_input" type="text" value="" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;"/>
   </p>
   <p><label for="email" class="form_label">Email Address</label><br/>
      <input  id="email" maxlength="80" name="email" class="form_input" type="text" value="" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;"/>
   </p>
   <p><label for="phone" class="form_label">Phone Number</label><br/>
      <input  id="phone" maxlength="40" name="phone" class="form_input" type="text" value="" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;"/>
   </p>
   <input type="hidden" id="fax_number" value="">
   <div class="g-recaptcha" data-sitekey="6Le8wh8UAAAAAJ6_kahoP82IbvVROpjf0iN6Kraw"></div>
   <div id="result"></div>
   <input type="submit" name="submit" id="submit" class="submit">
</form>




THIS COULD BE USED FOR AN AJAX SUBMISSION 
<script>

$(document).ready(function() {

	$("#leadform").submit(function(event) {
	console.log("SUBMITTED") ;
		 var values = $(this).serialize();
	    /* Stop form from submitting normally */
	    event.preventDefault();
	
	    /* Clear result div*/
	    $("#result").html('');
	
	    /* Get from elements values */
	    var values = $(this).serialize();
	
	    /* Send the data using post and put the results in a div */
	    /* I am not aborting previous request because It's an asynchronous request, meaning 
	       Once it's sent it's out there. but in case you want to abort it  you can do it by  
	       abort(). jQuery Ajax methods return an XMLHttpRequest object, so you can just use abort(). */
	       ajaxRequest= $.ajax({
	            url: "salesforce_php.php",
	            type: "post",
	            data: values
	        });
	
	      /*  request cab be abort by ajaxRequest.abort() */
	
	     ajaxRequest.done(function (response, textStatus, jqXHR){
	          // show successfully for submit message
	          console.log("SUCCESS") ;
	          $("#result").html(response);
	     });
	
	     /* On failure of request this function will be called  */
	     ajaxRequest.fail(function (){
	
	       // show error
	       console.log("ERROR") ;
	       $("#result").html('There is error while submit');
	     });
	
	});
	
});
</script>