Creating a lead capture form for your website

From SugarCRM Wiki

Jump to: navigation, search

Note: As of SugarCRM 4.5.1 there is additional lead capture functionality that is campaign related - see Campaign module -> Create Lead Form.

The following steps are required to setup lead capture in SugarCRM:

  1. Create the lead capture form
  2. Configure the lead capture script
  3. Configure redirection after form submission


Create the lead capture form

The lead capture form is a standard HTML form that can be customized any way you want. A sample lead capture form, "examples/LeadCapture.php", is provided with your Sugar installation. The sample lead capture form is a PHP script that dynamically generates the HTML form. The form can also be created as a simple static HTML page which we will do now.

To create the form properly we need the following information:

  • The location of our sugar installation on the world wide web (e.g. http://www.domain.com/sugarcrm)
  • The fields to be populated by our form (e.g. First Name, Last Name, Description, Lead Source)
  • A randomly chosen name to identify our form submission to our sugar installation (e.g. my_lead_capture_form)

With this information in hand we create a standard HTML form.


<html>
<body>
Welcome to my awesome lead capture form!<br>
<br>
<br>
<form name="lead_capture" action="http://www.domain.com/sugarcrm/leadCapture.php" method="post">
<input type='hidden' name='redirect' value='http://www.sugarcrm.com'>
<input type='hidden' name='lead_source' value='Web Site'>
<input type='hidden' name='user' value='my_lead_capture_form'>
First Name: <input type='text' name='first_name'> <br>
Last Name: <input type='text' name='last_name'> <br>
Description:    <select name="description">
                    <option value="Sugar is good">Sugar is good</option>
                    <option value="Sugar is great">Sugar is great</option>
                    <option value="Sugar is the greatest">Sugar is the greatest</option>
                </select><br>
<input type='Submit' name='submit' value='Submit' >
</form>
</body>
</html>


Note that this is a standard HTML form using standard HTML elements. Some items of note:

  • We've provided form elements for each field we would like to capture
  • We've used a drop down list to populate the description field
  • We've set the form action to point to our Sugar installation URL
  • We've added a hidden field called "redirect" which we will make use of later

This completes the lead capture form and now we have to tell Sugar how to handle the incoming form data.

Configure the lead capture script

So how does Sugar know what to do with form data submitted through a lead capture form?

The file "leadCapture.php" is the script responsible for processing the incoming data and creating the lead record. The script looks for a username and password associated to the submitted form data by checking for the "user" parameter. If we look back at our HTML form we can see we have set the "user" parameter to the value "my_lead_capture_form".


<input type='hidden' name='user' value='my_lead_capture_form'>


You can set this to any value you want as long as you provide a corresponding username and password for the script to process. We supply the username and password by creating the file "leadCapture_override.php" in our Sugar installation root directory (this is the same location as "leadCapture.php"). Place the following lines of code in the new file:


<?
$users = array(
    'my_lead_capture_form' => array(    'name' => 'USERNAME GOES HERE',
                                        'pass' => 'PASSWORD HASH GOES HERE' ));
?>


Replace 'USERNAME GOES HERE' with an active Sugar user and 'PASSWORD HASH GOES HERE' with the corresponding password hash. You can determine the password hash in two ways.

1. Retrieve it from the user table in the Sugar database:


SELECT `user_name`, `user_hash` FROM `users` WHERE `user_name` = 'YOUR_DESIRED_REC_ID';


2. Calculate it manually using an MD5 generator. For example the following site provides a JavaScript based MD5 generator:


http://hash-it.net/


Entering text into the "text" field will generate the corresponding MD5, SHA1, SHA256. For the 'pass' field in the leadCapture_override.php file we are interested in the MD5 value of the users password.


Our lead capture script is now ready to accept form submissions. All that's left is the redirect.

Configure redirection after form submission

Two methods of redirect are available. By default the form data will be POSTed to the URL supplied via the "redirect" parameter which we noted in the first section of this article. The redirection is handled by the following portion of code from "leadCapture.php":


    if(isset($_POST['redirect']) && !empty($_POST['redirect'])){
        //header("Location: ".$_POST['redirect']);
        echo '<html><head><title>SugarCRM</title></head><body>';
        echo '<form name="redirect" action="' .$_POST['redirect']. '" method="POST">';

        foreach($_POST as $param => $value) {

            if($param != 'redirect') {
                echo '<input type="hidden" name="'.$param.'" value="'.$value.'">';
            }
        }
        if( ($return_val == '') || ($return_val  == 0) || ($return_val < 0) ) {
            echo '<input type="hidden" name="error" value="1">';
        }
        echo '</form><script language="javascript" type="text/javascript">document.redirect.submit();</script>';
        echo '</body></html>';
    } else {
		echo "Thank You For Your Submission.";
    }


You can also configure the script to simply redirect without POSTing the form data by uncommenting the first line in the if-else block and commenting out the rest. For example:


    if(isset($_POST['redirect']) && !empty($_POST['redirect'])){
        header("Location: ".$_POST['redirect']);
        /* Comment out this portion
        echo '<html><head><title>SugarCRM</title></head><body>';
        echo '<form name="redirect" action="' .$_POST['redirect']. '" method="POST">';

        foreach($_POST as $param => $value) {

            if($param != 'redirect') {
                echo '<input type="hidden" name="'.$param.'" value="'.$value.'">';
            }
        }
        if( ($return_val == '') || ($return_val  == 0) || ($return_val < 0) ) {
            echo '<input type="hidden" name="error" value="1">';
        }
        echo '</form><script language="javascript" type="text/javascript">document.redirect.submit();</script>';
        echo '</body></html>';
        */
    } else {
		echo "Thank You For Your Submission.";
    }


If you do not supply a "redirect" parameter in your lead capture form Sugar will simply echo out "Thank You For Your Submission."


That concludes our setup of the lead capture functionality.

Personal tools