0

我在 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 完成,但我不知道该怎么做...谢谢!

4

1 回答 1

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

于 2014-04-23T08:58:41.653 回答