APIs & Integrations

zendersto
Member

Form API cannot populate information

SOLVE

Hi ive been using the form API to connect the data to Hubspot, im using PHP. Im testing the integration with a simple form via xampp. 

when I hit submit button with POST method, the form only sent to hubspot what page the data was sent from but not the information of the users. 

the code throws me an error with unknown hubspotutk so im quite sure it has something to do with cookie but i have no idea how to fix it.

here is my html form:

<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body>
<form action="hubspot.php" method="post">
  First name:<br>
  <input type="text" name="firstname"><br>
  Last name:<br>
  <input type="text" name="lastname" ><br>
  Email:<br>
   <input type="email" name="email" ><br><br>
  <input type="submit" value="Submit">
</form>

</body>
</html>

and here is my PHP code:

<?php
   //Process a new form submission in HubSpot in order to create a new Contact.
$firstname = $lastname = $email ="";
$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://localhost/mother.html',
    'pageName' => 'Zen Test'
);
$hs_context_json = json_encode($hs_context);

//Need to populate these variable with values from the form.
$str_post = "firstname=" . urlencode($firstname)
    . "&lastname=" . urlencode($lastname)
    . "&email=" . urlencode($email)
    . "&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/6123547/0051538d-2665-4988-8a18-2c449de75ae3';

$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.
$status_code = @curl_getinfo($ch, CURLINFO_HTTP_CODE); //Log the response status code
@curl_close($ch);
echo $status_code . " " . $response;

?>

what am i missing ?

0 Upvotes
3 Accepted solutions
himanshurauthan
Solution
Thought Leader | Elite Partner
Thought Leader | Elite Partner

Form API cannot populate information

SOLVE

Hello @zendersto 

 

It seems that the code that you have used for the form submission is working fine. The only thing that I have noticed in the code snippet is that you have made the email variable empty ( "" ) string and it can be cause of in proper data.

Try once with valid email value and it will work for sure.

 

Thanks

Digital Marketing & Inbound Expert In Growth Hacking Technology

View solution in original post

0 Upvotes
zendersto
Solution
Member

Form API cannot populate information

SOLVE

Hi thanks for your response, 

I replaced the variables now the code works fine and successfully capture the data and send to hubspot. However, it still throws me an error with hubspotutk, looks like I have to add something to the cookie ? can you help 

 

<?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://localhost/mother.html',
    'pageName' => 'Zen Test'
);
$hs_context_json = json_encode($hs_context);

//Need to populate these variable with values from the form.
$firstname = $_POST["firstname"];
$lastname = $_POST["lastname"];
$email= $_POST["email"];
$str_post = "firstname=" . urlencode($firstname)
    . "&lastname=" . urlencode($lastname)
    . "&email=" . urlencode($email)
    . "&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/6123547/0051538d-2665-4988-8a18-2c449de75ae3';

$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.
$status_code = @curl_getinfo($ch, CURLINFO_HTTP_CODE); //Log the response status code
@curl_close($ch);
echo $status_code . " " . $response;

?>

View solution in original post

0 Upvotes
WendyGoh
Solution
HubSpot Employee
HubSpot Employee

Form API cannot populate information

SOLVE

Hi @zendersto,

 

When looking to use the hubspotutk cookie, you've got to first installed the HubSpot tracking code (Learn more here: Install the HubSpot tracking code). This is because the hubspotutk cookie is placed in the user's browser by the HubSpot tracking code. This tracking code must be installed on the page that the form is on.

  • To verify if the HubSpot tracking code has been installed and that it's firing correctly, you can refer to this useful documentation: verify HubSpot tracking code.

In this case, could you confirm that with the tracking code present, you're seeing the error?

View solution in original post

0 Upvotes
4 Replies 4
himanshurauthan
Solution
Thought Leader | Elite Partner
Thought Leader | Elite Partner

Form API cannot populate information

SOLVE

Hello @zendersto 

 

It seems that the code that you have used for the form submission is working fine. The only thing that I have noticed in the code snippet is that you have made the email variable empty ( "" ) string and it can be cause of in proper data.

Try once with valid email value and it will work for sure.

 

Thanks

Digital Marketing & Inbound Expert In Growth Hacking Technology
0 Upvotes
zendersto
Solution
Member

Form API cannot populate information

SOLVE

Hi thanks for your response, 

I replaced the variables now the code works fine and successfully capture the data and send to hubspot. However, it still throws me an error with hubspotutk, looks like I have to add something to the cookie ? can you help 

 

<?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://localhost/mother.html',
    'pageName' => 'Zen Test'
);
$hs_context_json = json_encode($hs_context);

//Need to populate these variable with values from the form.
$firstname = $_POST["firstname"];
$lastname = $_POST["lastname"];
$email= $_POST["email"];
$str_post = "firstname=" . urlencode($firstname)
    . "&lastname=" . urlencode($lastname)
    . "&email=" . urlencode($email)
    . "&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/6123547/0051538d-2665-4988-8a18-2c449de75ae3';

$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.
$status_code = @curl_getinfo($ch, CURLINFO_HTTP_CODE); //Log the response status code
@curl_close($ch);
echo $status_code . " " . $response;

?>
0 Upvotes
himanshurauthan
Thought Leader | Elite Partner
Thought Leader | Elite Partner

Form API cannot populate information

SOLVE

Hello @zendersto 

 

If you can please write me the specific error that you are getting related to hubspotutk parameter then I can help you better.

 

Thanks

Digital Marketing & Inbound Expert In Growth Hacking Technology
0 Upvotes
WendyGoh
Solution
HubSpot Employee
HubSpot Employee

Form API cannot populate information

SOLVE

Hi @zendersto,

 

When looking to use the hubspotutk cookie, you've got to first installed the HubSpot tracking code (Learn more here: Install the HubSpot tracking code). This is because the hubspotutk cookie is placed in the user's browser by the HubSpot tracking code. This tracking code must be installed on the page that the form is on.

  • To verify if the HubSpot tracking code has been installed and that it's firing correctly, you can refer to this useful documentation: verify HubSpot tracking code.

In this case, could you confirm that with the tracking code present, you're seeing the error?

0 Upvotes