This text is replaced by the Flash movie.

SF forms with captcha

Adding captcha with web-to-lead forms

There are times you want to avoid those annoying bots filling your forms and putting garbage into your data with in your salesforce leads, there are measures to avoid this and if you prefer using captcha, you might want to use reCaptcha. If you know how to code with salesforce apex this link will already solve your problem. But for those who prefer using the simple web-to-lead form you can use CURL with PHP to reprocess the data being submitted and submit it to salesforce. In this example I'm using PHP with cURL and ReCaptcha. This is just basic PHP coding.


The Basic Form

First is the sf form created by Web-to-Lead on App Setup and editing some parts

In action form https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8 change this to the php page you want to reprocess the data

Some of the fields I used are custom fields for some reason salesforce generate name with number at the beginning which will cause a lot of trouble with javascript and php when trying to use it with in your code.

So you need also to rename some input datafield with name has number at the beginning, in this case 00N40000001oHG5 I change it to From_Link and 00N40000001pyzT for Region

Add the recaptcha field at the end of your form before submit.
<? echo recaptcha_get_html($publickey); ?>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<form action="check-this-first.php" method="post"  name="emailForm" id="test" style="padding: 0px; margin: 0px;" >
<input type=hidden name="oid" value="00D40000000IUAQ">
<input type=hidden id="From_Link" maxlength="255" name="From_Link" value="<?=$cameFrom ?>"  /> <!-- came from link  //-->
<input type=hidden id="Campaign_ID" name="Campaign_ID" value="701400000005waD"> <!-- Google Ad Words //-->
<input type=hidden id="recordType" name="recordType" value="0124000000014Ed"> <!-- Google Ad Words //-->
<input type=hidden name="retURL" value="http://www.thankyoupage.com/merci-beaucoup">
  <table>
    <tr>
      <td >First Name: <br />
      <input  style="width:200px;" id="first_name" maxlength="40" name="first_name" size="20" type="text"/>
        </td>
    </tr>
    <tr>
      <td >Last Name: <br />
      <input  style="width:200px;" id="last_name" maxlength="80" name="last_name" size="20" type="text" />
        </td>
    </tr>
    <tr>
      <td >Email:<br />
        <input  style="width:200px;" id="email" maxlength="80" name="email" size="20" type="text" /></td>
    </tr>
 
     <tr>
      <td >Company:<br />
      <input  style="width:200px;" id="company" maxlength="40" name="company" size="20" type="text" />
        </td>
    </tr>
 
 
 
    <tr>
      <td >Country: <br />
        <input  style="width:200px;" id="country" maxlength="40" name="country" size="20" type="text" />
       </td>
    </tr>
     <tr>
      <td >Region: <br />
<select  id="Region" name="Region" title="Region"><option value="North America">North America</option>
<option value="South America">South America</option>
<option value="EMEA">EMEA</option>
<option value="APAC">APAC</option>
</select></td>
    </tr>
 
 
      <tr>
 
      <td align="left" >
      <span class="style1 style2">Verify Code:<br />
 
        <br />
        <!-- Captcha//-->
        <?
 
echo recaptcha_get_html($publickey);
 
         ?> 
        <!-- Captcha //--></td>
    </tr>
 
    <tr>
 
      <td>
      <br /><br />
      <input type="submit" name="submit">
         </td>
    </tr>
  </table>
</form>



Applying the reCaptcha - STOP SPAM READ BOOKS(Seriously Spammers!)

Now After submitting the form it will check first if the captcha is right. You may need to put the code below - at the head of your php page where the data will be process first, or you might want to read this first on how to use reCaptcha with PHP http://code.google.com/apis/recaptcha/docs/php.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?
ob_start();
session_start();
 
include_once('recaptcha/recaptchalib.php');
 
         //Recaptcha Settings
       $publickey = "xxxx"; // you got this from the signup page
	   $privatekey = "xxxx";
 
       extract($_POST);
       if ($submit){  //if its sumbitted
 
        $resp = recaptcha_check_answer ($privatekey,
                                $_SERVER["REMOTE_ADDR"],
                                $_POST["recaptcha_challenge_field"],
                                $_POST["recaptcha_response_field"]);
 
		$ok = 1;
 
            if (!$resp->is_valid) { //if captcha is  invalid
			  $ok = 0;  
			}
 
	        if ($ok){	
               //Put here the data for sf leads storing
            }
            else {
			  // Message here that the captcha was invalid 
			}	
       } 
 
?>



Appying cURL

Now appying Using cURL, to be able to do this - your server should be running cURL library, taking the idea from David Walsh on how to post via cURL method http://davidwalsh.name/execute-http-post-php-curl, notice on $fields array I used the field name 00N40000001oHG5 for $From_link and 00N40000001pyzT for $Region, so once the data are being submitted it will check first in this sample php code before submitting it to salesforce's site.

So here is the full listing for submission to salesforce site.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<? 
 
ob_start();
session_start();
 
include_once('recaptcha/recaptchalib.php');
 
         //Recaptcha Settings
       $publickey = "xxxx"; // you got this from the signup page
	   $privatekey = "xxxx";
 
 
	//curl method posting
	//extract data from the post
      extract($_POST);
 
		if ($submit){
 
		$ok = 1;
 
		 $resp = recaptcha_check_answer ($privatekey,
                                $_SERVER["REMOTE_ADDR"],
                                $_POST["recaptcha_challenge_field"],
                                $_POST["recaptcha_response_field"]);
 
		 if (!$resp->is_valid) {
			  $ok = 0;
			}
 
	if ($ok){		
		//set POST variables
		$url = 'https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8';
		$fields = array(
					'oid'=>urlencode($oid),
					'Campaign_ID'=>urlencode($Campaign_ID),
					'00N40000001oHG5'=>urlencode($From_Link),
					'recordType'=>urlencode($recordType),
					'retURL'=>urlencode($retURL),
					'first_name'=>urlencode($first_name),
					'last_name'=>urlencode($last_name),
					'email'=>urlencode($email),
					'company'=>urlencode($company),
					'00N40000001pyzT'=>urlencode($Region)
				);
 
		//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);
 
	} //if ok
	else {
			  echo "<h4>Sorry - Invalid Captcha </h4>";
			}	
 
 
 } //if submit.
 ?>





Tags: , , , , ,

8 Responses to “SF forms with captcha”

  1. Hi there,

    I am beginner in php and honestly I do not know much about it. I have to solve this problem to add CAPTCHA in the web to lead form, I do not know where I must put this code in what page? if you do not mind please send me a project that is working like above.

    Best regards

    Safoura Mazaherifar
    July 4th, 2011 at 4:42 am
  2. Hello and many thanks for this.

    I’ve followed the steps one-by-one, the captcha is showing up in the form, but, when the form is submited, the “check-this-first.php” only shows a blank page, and nothing happens.

  3. My mistake, there was an error in my file.
    Thanks.

  4. Hi,

    I tried your code it works fine…but I have one doubt….I have area of interest field…which is select with multiple option field..how do I pass that data from php to salesforce….if I use your code..only last value selected goes into the database.

  5. Super article !!!

    thank you thank you.

    I have managed to make it working, but only with 2 field types I don’t get the required data. These are custom fields, one is a text box, and other is a multiple selection list.

    Can you think of a reason why is this not working with them?

  6. Sorry I just got back and read through comments, Probably you need to name the custom field of your main form to something PHP will understand, in this article’s case for example ‘00N40000001oHG5′=>urlencode($From_Link), my custom field on salesforce is named 00N40000001oHG5 I renamed this to From_Link on main form and before submitting to SF leads pass it back to ‘00N40000001oHG5′, Let me know if it helped.

  7. Sorry for so late reply – I will try to post the source code.

Leave a Reply