API Submit Form Data redirectUrl not working


#1

Using PHP to pass a redirect url within the hs_context parameter isn’t working. I’m getting a 302 response code and my data is successfully being added to HS but the redirect isn’t working.
I even tested it with my redirect shut off in the form UI in HS.

Here’s a snippet:

$hs_context = array(
            'hutk' => $hubspotutk,
            'ipAddress' => $ip_addr,
            'pageUrl' => 'http://currenturl.com',
            'pageName' => 'Test Page',
            'redirectUrl' => 'http://dev.skodaminotti.com/marketing/thanks/'
        );

hs_context is then passed JSON encoding, urlencoded and sent. What could be the issue?


#2

Hi @Andrew_Taucher,

If you have a redirect URL set up for the form in the UI, it will override this option, even if no redirect URL is included in the form POST. Can you send me a link to the page where this form is?


#3

The form is at the bottom of this page http://dev.skodaminotti.com/marketing/about-us/
I did tests with the redirect URL shut off in the form UI but still got the same result.

The only thing I can think of is HS cache’s these types of settings. I’ve had caching issues with HS blog email templates before too.


#4

Hi @Andrew_Taucher,

Sorry for the delay on this, and thank you for your patience. How are you handling the form submission server side? The redirectUrl just changes the response you see from the HubSpot API; you’ll see a 204 if there’s no redirectUrl in the submission or the UI, and a 302 with a location header (corresponding to the redirectUrl) if there is. How are you handling the response on your server?


#5

Here’s my full script. I’m not seeing anything for $response in my echo, just the 302 status code after submission. Nor is there anything in my network tab for the 302 in dev tools.

<?php
    if (isset($_POST['submit'])) {
        // Set all form variables
        $fname = $_POST['firstname'];
        $lname = $_POST['lastname'];
        $email = $_POST['email'];
        $phone = $_POST['phone'];
        $msg = $_POST['message'];
        //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://currenturl.com',
            'pageName' => 'Test Page',
            'redirectUrl' => 'http://dev.skodaminotti.com/marketing/thanks/'
        );
        $hs_context_json = json_encode($hs_context);

        //Need to populate these variable with values from the form.
        $str_post = "firstname=" . urlencode($fname) 
            . "&lastname=" . urlencode($lname) 
            . "&email=" . urlencode($email) 
            . "&phone=" . urlencode($phone) 
            . "&message=" . urlencode($msg) 
            . "&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/136649/f28cab84-548b-4d68-b36c-13425be770c2';

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

I assumed I could just use PHP header($response)?


#6

Hi @Andrew_Taucher,

I’m not particularly familiar with PHP, so I’m not really the best resource to help debug this script. The 302 response you get from HubSpot should include a location header which will contain the redirect URL. You could also parse the request you get from the client for the redirectUrl parameter and respond accordingly, rather than waiting for the response from HubSpot.