Hi. I'm looking for a way to perfrom a recaptcha verification pass on an html form then, if successful, submit the detail to salesforce via their Web2Lead feature (don't have API access unfortunately).

Our salesforce integrated contact form has been subject to spam, like many others it seems, therefore I have incorporated recaptha to better verify the human touch. However, having altered the form action to use process.php (my working recaptcha verification script) I am now completely stumped as to how I then submit the detail to Web2Lead when the recaptcha check is successful. The only way I understand to do this is via the below form action which I have had to replace so as to enable the recaptcha check:

action=""

Can I submit the detail from the process.php file in some way or maybe have a new hidden form populated using the original filled-out detail then have this new form auto-submitted the above code as its action?

Process.php file

<?php
session_register();
	session_start();                      
	$firstname = $_POST['first_name'] ;
	$_SESSION['firstname'] = $firstname;
	$lastname = $_POST['last_name'] ;
	$_SESSION['lastname'] = $lastname;
	$email = $_POST['email'] ;
	$_SESSION['email'] = $email;
	$phone = $_POST['phone'] ;
	$_SESSION['phone'] = $phone;
	$company = $_POST['company'] ;
	$_SESSION['company'] = $company;
	$URL = $_POST['URL'] ;
	$_SESSION['URL'] = $URL;
	$description = $_POST['description'] ;
	$_SESSION['description'] = $description;	


require_once('recaptchalib.php');
$privatekey = "...PRIVKEY";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if (!$resp->is_valid) {
header("location:request_submitted.php?result=fail");
die();
}

else if ($resp->is_valid) {
header("location:request_submitted.php?result=pass");
}

?>

At the moment, the recaptcha is checked and you are then directed to the above header page with success or failure notifications to the user which is great !)()! (free sarcmark!). However, with no info now ending up in salesforce, my form pretty much of 'zero use to my business'. Doh!

Any help or guidance will be really appreciated. TIA

Dani AI

Generated

— two practical ways to get the form data into Salesforce after your reCAPTCHA check: (A) do a server-side POST to the Web‑to‑Lead endpoint (recommended), or (B) re-render the original Web‑to‑Lead form with hidden inputs and auto‑submit it in the browser. was right to say the submit work belongs in the success branch — don’t redirect away before you send the lead. Below are concise, safe examples and a few caveats.

Server-side (recommended): build an array of the lead fields, add your Salesforce org id and a return URL, url‑encode the payload and POST it with cURL. This keeps the user on your site, lets you log success/fail, and retry or queue a lead if Salesforce is temporarily unavailable.

<?php
// after verifying reCAPTCHA
$fields = [
  'first_name' => $_POST['first_name'],
  'last_name'  => $_POST['last_name'],
  'email'      => $_POST['email'],
  'company'    => $_POST['company'],
  'oid'        => '00Dxxxxxxxxxxxx',        // your Salesforce org id
  'retURL'     => 'https://yourdomain/thanks' // where SF should return
];

$payload = http_build_query($fields);
$ch = curl_init('SF_WEBTOLEAD_ENDPOINT'); // replace with your Web‑to‑Lead endpoint
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/x-www-form-urlencoded']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
$response = curl_exec($ch);
$http = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$err = curl_error($ch);
curl_close($ch);

// log $http / $err, then redirect to your local thank-you page
header('Location: /request_submitted.php?result=pass');
exit;
?>

Client-side (simpler): output an HTML form populated with hidden inputs (sanitize with htmlspecialchars) and auto-submit it via JavaScript. Works quickly but depends on the client and exposes the redirect flow to Salesforce; set the return URL to your own thank‑you page so the user comes back.

<form id="sfdc" action="SF_WEBTOLEAD_ENDPOINT" method="post">
  <input type="hidden" name="first_name" value="<?php echo htmlspecialchars($first_name,ENT_QUOTES); ?>">
  <!-- more hidden inputs... -->
</form>
<script>document.getElementById('sfdc').submit();</script>

Extra notes: don’t call session_register() (use session_start() and $_SESSION), always sanitize user input to avoid XSS, log errors from cURL and fall back (store lead locally or email it) if the Web‑to‑Lead POST fails, and make sure reCAPTCHA verification completes before you send anything to Salesforce. — if you need a short debug checklist (required fields, oid, retURL, SSL/cURL errors), say which PHP version you’re on and I’ll keep it tight.

Recommended Answers

All 3 Replies

Why not put original mail process code between:

else if ($resp->is_valid) {
//....mail process code goes here...before re-direction
header("location:request_submitted.php?result=pass");
}

The mail code itself, should not have any other redirection functions. As the line header("location:request_submitted.php?result=pass"); won't be called from the new page, unless the new page includes that line of code.

Thanks for the suggestion, however a mail function here is no good for me. Salesforce Web2Lead can only accept info from a form with the action set as below:

<form action="" method="POST">

Is there somethiing else I could use at this success point in the script so as to regenerate the form and its detail with the above salesforce action set, then ideally submit the form automatically?

Thanks.

Hey, did you have success with this?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.