web to lead form background/hidden - Answers - Salesforce Trailblazer Community
Trailblazer Community
Ask Search:
Enrico Maria BalattiEnrico Maria Balatti 

web to lead form background/hidden


we have a lot of form, on our company site but also on sponsor/partner site.
This form (e.g. contactus form) after submission, redirect the user to a thank-you page: hidden paramenters are sent to the thank-you page because these parameters are needed for other task/procedure/calls.

We would like to make all these form as salesforce web2lead form but we would like to maintain also the parameter sent to the thank-you page, in order that the as-is procedure will yet work on that page.

I am trying to test the form page with two forms: one the as-is form and the other the salesoforce web2lead form (hidden form). Using javascript/ajax, when the user submit the existing form, the same variable (firstname, lastname,..) are copied on the web2lead form attrbute and  I try to submit both the form by javascript (they have different action post). For example I am trying solution like http://ctrlq.org/code/19233-submit-forms-with-javascript.
But this is not working.
How I can achieve the goal?Do you have atoer solution in mind?

Thank you for you support.
Sachin DasSachin Das
I happen to find few articles from few blogs. I have collated and tried providing you the best. Please let me know if this helps:

First, make sure that you have web-to-lead enabled for your organization, pick the fields you want to include on your form, and then copy the HTML generated by Salesforce.

The trouble with the HTML that Salesforce gives you is twofold: 1) it posts the data directly to Salesforce, so you don't get a chance to check it for errors, and 2) it puts your organization's ID directly in the HTML form, so anyone can see it by viewing your page source, putting you at the mercy of spammers.

Problem 1: Error Checking

People make mistakes. There is no getting around that, but with a bit of effort, you can prevent lead information with obvious typos from being input via your web-to-lead form. One great example of where an obvious typo might crop up is the email field, which you will very likely want on your form. There are a couple of ways you could check an email address's validity: with a bit of client-side Javascript or with a server side script, using a language like PHP. Your Javascript (assuming you're using jQuery) might look like this:

<!-- HTML -->
<form action="#" id="web-to-lead-form" method="POST">
    <div id="formError" style="display:none;"></div>
    <input type="text" name="email" id="email" />
    <div id="emailError" style="display:none;"></div>



<!-- Javascript -->
<script type="text/javascript">
    function is_email(email) {
        //Email validation code goes here
    $("#web-to-lead-form").submit(function() {

        var email = $("#emailField").val();
        if (!is_email(email)) {
            $("#formError").html("Please correct the errors below.");
            $("#emailError").html("Please enter a valid email address.");

You can add server side code instead of or in addition to the Javascript checks as well. If you are using server side code, you will need to be able to post the form data to Salesforce from the script. More on that in the next section.

One other good idea is to change the Salesforce provided text inputs to selects instead. Selects are dropdown menus, and they allow you to force users to choose from a certain set of options, instead of being able to type whatever they want. This is really useful for fields containing data like states or provinces, where one user might type "New York" and another might type "NY." If you are like me, you really dislike it when different formats are used to mean the same thing, cluttering your database with non-standardized data. With a dropdown menu, you ensure that the same format is used every time.

Problem 2: The Spam Risk

The biggest problem, in my opinion, with the standard web-to-lead form is that it puts your Salesforce organization at risk to attacks by spammers, by exposing your organization ID to the world. One way around this is to set the form action to a script on your web server, set the organization ID field from within the script (so that no one else can see it), append it to the other fields that the user already filled  out, and then post the data to Salesforce from there. From the users' perspective, nothing changes with this approach, but it enhances your protection from  spammers.

I will give a quick example of implementing a solution like this, using PHP. First of all, you'll need to make sure that you have the cURL PHP extension installed (to allow you to perform HTTP post requests from PHP), and then you should be good to go. Here is an example, assuming you have a form that accepts inputs for First Name, Last Name, and Company:

function post_lead() {
    $url = 'https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8';
    $firstname = $_POST['first_name'];
    $lastname = $_POST['last_name'];
    $company = $_POST['company'];

    $params = 'first_name=' . $firstname .
              '&last_name=' . $lastname .
              '&company=' . $company .
              '&oid=' . $oid;
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $params);

    $json_response = curl_exec($curl);

    $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);

    if ($status != 200) {
        die("Error - call failed with status " . $status . " response " .
            $json_response . " curl error " . curl_error($curl) . " curl error number " .


    return json_decode($json_response, true);

Enrico Maria BalattiEnrico Maria Balatti
Hello Sachin, Thank you for your reply. Error checking and reduce spam risk are two important things to take into consideration during web2lead implementation. Your suggestion are very hwlpful for that. Regarding the issue to have a double concurrent submission (one for the web2lead and the other to the thankyou page passing same parameters), have you any suggestion? My goal is to add web2lead to actual forms, so lead are created in salesforce, but on the web site the form action should also works as now, that is passing parameter to the thankyou page because on that page some process is working. BR Enrico
Sachin DasSachin Das
In order for the system to link to the correct salesforce.com field, you would have to actually use the Field ID.  You can get this by looking at the URL when you view the field in salesforce.com.  

<input type="hidden" id="00N70000002PRxz" name="00N70000002PRxz" value="Newsletter" />
<input type="hidden" id="00N70000002PRy4" name="00N70000002PRy4" value="http://www.aerometric.com/services/aerial-photography/" />

Sachin DasSachin Das
You could use a hidden field on your Web-To-Lead for to set the value of the record type on creation. Here some help on creating hidden fields with preset values:

When using salesforce.com's Web to Lead and Web to Case, you can use hidden HTML parameters to set the values of your lead or case fields. The use of hidden parameters prevents your end-users from changing the value of those fields.

To pass a hidden value, use the following HTML tags:

<input type=hidden name="nameofhiddenfield" id="nameofhiddenfield" value="XXXX">

For example, if you want all cases captured via the Online Case Capture interface to have a Priority of High, then you would use the following HTML syntax:

<input type=hidden name="Priority" id="Priority" value="High">

Custom fields can also be passed as hidden fields. However, this may require assistance from the Administrator of the Organization to attain the ID number of the custom field. The correct syntax to pass a hidden custom field would then be:

<input type=hidden name="00N00000000X0xY" id="00N00000000X0xY" value="CustField">
David GarciaDavid Garcia
@Enrico did you ever figure out how to pass those fields in the URL to another page?