将表单数据提交到Hubspot API和Expression Engine数据库

时间:2013-09-19 18:52:22

标签: php jquery ajax expressionengine hubspot

我在Expression Engine网站上有一个基本的HTML表单,看起来像这样:

<form action="http://www.autouplinktech.com/?ACT=28" accept-charset="utf-8" method="post">
    <input type="text" name="first_name" id="first_name" placeholder="{freeform:label:first_name} *">
    <input type="text" name="last_name" id="last_name" placeholder="{freeform:label:last_name} *">
    <input type="text" name="email" id="email" placeholder="{freeform:label:email} *">
    <input type="text" name="phone" id="phone" placeholder="{freeform:label:phone} *">
    <input type="text" name="company" id="company" placeholder="{freeform:label:company} *">
</form>

我需要保留提交到主数据库的能力,还要将数据发布到hubspot.php页面,该页面通过其API发送数据。 hubspot.php看起来像这样:

<?php
//Process a new form submission in HubSpot in order to create a new Contact.

$hubspotutk = $_COOKIE['hubspotutk'];  //grab the cookie from the visitors browser.
$ip_addr = $_SERVER['REMOTE_ADDR'];  //IP address too.
$hs_context = array(
        'hutk' => $hubspotutk,
        'ipAddress' => $ip_addr,
        'pageUrl' => 'http://www.example.com/form-page',
        'pageName' => 'Example Title'
    );
$hs_context_json = json_encode($hs_context);

//Need to populate these varilables with values from the form.
$str_post = "firstname=" . urlencode($first_name)
        . "&lastname=" . urlencode($last_name)
        . "&email=" . urlencode($email)
        . "&phone=" . urlencode($phone)
        . "&company=" . urlencode($company)
        . "&hs_context=" . urlencode($hs_context_json);  //Leave this one be :)

 //replace the values in this URL with your portal ID and your form GUID
$endpoint = 'https://forms.hubspot.com/uploads/form/v2/{portalId}/{formGuid}';

$ch = @curl_init();
@curl_setopt($ch, CURLOPT_POST, true);
@curl_setopt($ch, CURLOPT_POSTFIELDS, $str_post);
@curl_setopt($ch, CURLOPT_URL, $endpoint);
@curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
@curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = @curl_exec($ch);  //Log the response from HubSpot as needed.
@curl_close($ch);
echo $response;

?>

我很确定这可以用AJAX和jQuery完成,但我不知道该做什么......谢谢!

1 个答案:

答案 0 :(得分:0)

我不知道这对你有帮助,因为为时已晚。无论如何,我发布这个。

<script>
    $(document).ready(function(){
        $('#send_button').click(function(e){
            e.preventDefault();
            var res="";
            x=$("form").serializeArray();
            $.each(x, function(i, field){
                res+=i>0?"&":"";
                res+=field.name + "=" + field.value;
            });                
            $.ajax({
                type:'hubspot.php',
                url: url,
                data:res
            });
        });
    });
</script>
<form action="http://www.autouplinktech.com/?ACT=28" accept-charset="utf-8" method="post" id="form1">
    <input type="text" name="first_name" id="first_name" placeholder="{freeform:label:first_name} *">
    <input type="text" name="last_name" id="last_name" placeholder="{freeform:label:last_name} *">
    <input type="text" name="email" id="email" placeholder="{freeform:label:email} *">
    <input type="text" name="phone" id="phone" placeholder="{freeform:label:phone} *">
    <input type="text" name="company" id="company" placeholder="{freeform:label:company} *">
    <a href="javascript:void(0)" id="send_button"><span>Submit</span></a>
</form>

然后在您的hubspot php中,您可以获得$_POST

中的所有值