function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Amanda Byrne- Carolina Tiger RescueAmanda Byrne- Carolina Tiger Rescue 

Using reCaptcha for a web to lead form

I work for a nonprofit, and I've been trying to adapt a web to lead form to use reCaptcha.  

I found a suggestion here: https://developer.salesforce.com/forums/ForumsMain?id=906F00000008sR4IAI
In summary, you modify the salesforce web-to-lead form to submit to a verify.php file you create on your web server, and then that file uses "curl" ro resubmit the data to the original POST URL for the Salesforce web-to-lead

I've done my best to merge the reCaptcha directions (https://developers.google.com/recaptcha/docs/php) with the suggestions in the form (http://malebolgia.shadowsonawall.net/code-programming/captcha-with-salesforce.html) above, but when I go to submit my web to lead form, the verify.php file shows this error:
Parse error: syntax error, unexpected T_LNUMBER, expecting T_VARIABLE or '$' in D:\Hosting\4899791\html\about\verify.php on line 31

Line 31 of verify.php sets the value of a variable (for a custom Salesforce Lead field) to the one passed from the form- syntax looks like all the other fields being passed.  I'll post the full code at the end of my post

Another thing I've noticed is that the verify.php file that has the error is posting the value of these fields into the URL, so that seems to be OK.  Here's the URL
http://carolinatigerrescue.org/about/verify.php?oid=00DA0000000A32t
&retURL=http%3A%2F%2Fcarolinatigerrescue.org%2Fabout%2Fsubscribe-thanks.asp
&AccountType=Individual
&company=Self
&first_name=Barack
&last_name=Obama
&street=
&city=Washington
&state=DC
&zip=10018
&country=United+States
&email=AmandaByrne%40CarolinaTigerRescue.org
&emailOptOut=0
&00NA000000A0fWR=Subscribed+by+Request
&00NA000000A0fWH=Subscribed+by+Request
&recaptcha_challenge_field=(removed for security)
&recaptcha_response_field=asnomic+Hig
&submit=Subscribe

Can someone help me figure out where my syntax has gone wrong?

Verify.PHP code
<?php
  require_once('../includes/recaptchalib.php');
  $privatekey = "(removed for security)";
  $resp = recaptcha_check_answer ($privatekey,
                                $_SERVER["REMOTE_ADDR"],
                                $_POST["recaptcha_challenge_field"],
                                $_POST["recaptcha_response_field"]);

  if (!$resp->is_valid) {

    die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
         "(reCAPTCHA said: " . $resp->error . ")");
  } else {
// CTR curl code to resubmit to Salesforce web to lead
//set POST variables
  $url = 'https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8';
  $fields = array(
     'oid'=>urlencode($oid),
     'recordType'=>urlencode($recordType),
     'retURL'=>urlencode($retURL),
     'first_name'=>urlencode($first_name),
     'last_name'=>urlencode($last_name),
     'company'=>urlencode($company),
     'street'=>urlencode($street),
     'city'=>urlencode($city),
     'state'=>urlencode($state),
     'zip'=>urlencode($zip),
     'country'=>urlencode($country),
     'email'=>urlencode($email),
     'emailOptOut'=>urlencode($emailOptOut),
     '00NA000000A0fWR'=>urlencode($00NA000000A0fWR),
     '00NA000000A0fWH'=>urlencode($00NA000000A0fWH),
    
   
    );

  //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);

  }
  ?>
Best Answer chosen by Amanda Byrne- Carolina Tiger Rescue
Amanda Byrne- Carolina Tiger RescueAmanda Byrne- Carolina Tiger Rescue
I'm all sorted out.

Just for the future reference of others- here's how the working code finally came out.  Be sure to create all the fields in verify php that you need forwarded.

<?php

ob_start();
session_start();

  require_once('../includes/recaptchalib.php');

         //Recaptcha Settings
       $publickey = "(removed for security)"; // you got this from the signup page
    $privatekey = "(removed for security)";


//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;
    echo("Verify is failing");
  }
if ($ok){ 
  //set POST variables
// CTR curl code to resubmit to Salesforce web to lead
//set POST variables
  echo("Verify is progressing 1");
  $url = 'https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8';
  $fields = array(
     'oid'=>urlencode($oid),
     'recordType'=>urlencode($recordType),
     'retURL'=>urlencode($retURL),
     'first_name'=>urlencode($first_name),
     'last_name'=>urlencode($last_name),
     'company'=>urlencode($company),
     'street'=>urlencode($street),
     'city'=>urlencode($city),
     'state'=>urlencode($state),
     'zip'=>urlencode($zip),
     'country'=>urlencode($country),
     'email'=>urlencode($email),
     '00NA0000009OvgF'=>urlencode($Preferred_Email),
     'emailOptOut'=>urlencode($emailOptOut),
     '00NA000000A0fWR'=>urlencode($Newsletter),
     '00NA000000A0fWH'=>urlencode($Mail_List),
     'lead_source'=>urlencode($lead_source),
    
   
    );

  //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{
  die ("<p>The reCAPTCHA wasn't entered correctly. Go back and try it again.<br>" .
         "(reCAPTCHA said: " . $resp->error . ")</p>"); }
} //if submit
  ?>

All Answers

Amanda Byrne- Carolina Tiger RescueAmanda Byrne- Carolina Tiger Rescue
on looking further- the problem could potentially be that PHP doesn't like variable names that start with numbers- if you agree, can you suggest a solution?
Amanda Byrne- Carolina Tiger RescueAmanda Byrne- Carolina Tiger Rescue
Moved further in the process- apparently there was indeed an issue with the custom object names.  I renamed those form elements as so:
        <input type="checkbox" id="00NA000000A0fWH" name="Mail_List" title="Mail List" value="Subscribed by Request" CHECKED />

and modified the lines for them in the verify.php
'00NA000000A0fWH'=>urlencode($Mail_List),

so now I'm having issues with reCaptcha not accepting my text as being correct- which it seems to be.  Would appreciate suggestions, but I suspect I'm facing a new problem now.
Amanda Byrne- Carolina Tiger RescueAmanda Byrne- Carolina Tiger Rescue
I'm all sorted out.

Just for the future reference of others- here's how the working code finally came out.  Be sure to create all the fields in verify php that you need forwarded.

<?php

ob_start();
session_start();

  require_once('../includes/recaptchalib.php');

         //Recaptcha Settings
       $publickey = "(removed for security)"; // you got this from the signup page
    $privatekey = "(removed for security)";


//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;
    echo("Verify is failing");
  }
if ($ok){ 
  //set POST variables
// CTR curl code to resubmit to Salesforce web to lead
//set POST variables
  echo("Verify is progressing 1");
  $url = 'https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8';
  $fields = array(
     'oid'=>urlencode($oid),
     'recordType'=>urlencode($recordType),
     'retURL'=>urlencode($retURL),
     'first_name'=>urlencode($first_name),
     'last_name'=>urlencode($last_name),
     'company'=>urlencode($company),
     'street'=>urlencode($street),
     'city'=>urlencode($city),
     'state'=>urlencode($state),
     'zip'=>urlencode($zip),
     'country'=>urlencode($country),
     'email'=>urlencode($email),
     '00NA0000009OvgF'=>urlencode($Preferred_Email),
     'emailOptOut'=>urlencode($emailOptOut),
     '00NA000000A0fWR'=>urlencode($Newsletter),
     '00NA000000A0fWH'=>urlencode($Mail_List),
     'lead_source'=>urlencode($lead_source),
    
   
    );

  //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{
  die ("<p>The reCAPTCHA wasn't entered correctly. Go back and try it again.<br>" .
         "(reCAPTCHA said: " . $resp->error . ")</p>"); }
} //if submit
  ?>
This was selected as the best answer
Ricky MartinRicky Martin
If you wish I will work this project,
Nim BarshadNim Barshad
I've tried this method and I'm getting a blank page as the result. Is there any issue with using a dropdown for a default text field?