PHP form won't submit


#1

I must be missing something, but not sure what. I can't get my form to submit. Below is the php code for the form submission. Any help is greatly appreciated.

<?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',
    'pageName' => 'Example Title'
);
$hs_context_json = json_encode($hs_context);

//Need to populate these variable with values from the form.
$str_post = "firstname=" . urlencode($_POST['fname']) 
    . "&lastname=" . urlencode($_POST['lname']) 
    . "&email=" . urlencode($_POST['email']) 
    . "&phone=" . urlencode($_POST['phone']) 
    . "&company=" . urlencode($_POST['company']) 
    . "&comments_alps__c=" . urlencode($_POST['hiddencomments']) 
    . "&lead_notes__c=" . urlencode($_POST['leadnotes']) 
    . "&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/hidden for posting/hidden for posting';

$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;

?>

#2

When you say 'won't submit' - what do you mean exactly? Does the form appear to submit but no data goes into HubSpot? Are there are any errors anywhere?

How are you sending the form data to the above script? Can you log the response to the console?


#3

It does not appear to submit. The url changes from the url with the form on it, to the url of the page with the above code, but the page is totally blank and the data does not show up in hubspot.

Here is the form tag:

<form action="hubspot_form_submit_risk.php" method="post">

hubspot_form_submit_risk.php contains the code above.

How would I log a response to the console. Sorry, I'm a newbie.


#4

OK. Could be any number of reasons. Is this a local site? What local server are you using? You need to ensure error reporting is on.


#5

Ok, I tried it on another server and got error code: 404. Not sure what that means, but it did something.


#6

I was wrong. I'm getting error code: 302


#7

UPDATE: The form is now submitting to hubspot, but the re-direct after the form submits is not working. I believe that's where the 302 error is coming from. Any insight into what is causing this?


#9

Does this look right?

$hs_context = array(
'hutk' => $hubspotutk,
'ipAddress' => $ip_addr,
'pageUrl' => 'http://form_submit_page.html',
'pageName' => 'Example',
'redirectUrl' => 'http://redirect_page.php.'
);


#10

Hi @bds9487,

If you're setting a redirect, HubSpot will return a 302 response (a 302 is a redirect). You should pass this along to the client; the 302 will redirect them to the page specified in the redirectUri.