0

I'm trying to post the html form data below to hubspot but when i have it deployed to either heroku or aws elastic beanstalk I receive 204 back instead of my data being posted to the crm. I used the solution found here how to execute php function on html button click

but have had no success getting the information needed into hubspot.

html file   
 <!DOCTYPE html>
    <html lang="en">
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
        <script type="text/javascript">
            function myAjax () {
                $.ajax( { type : 'POST',
                    data : { },
                    url  : 'index.php',              // <=== CALL THE PHP FUNCTION HERE.
                    success: function ( data ) {
                        alert( data );               // <=== VALUE RETURNED FROM FUNCTION.
                    },
                    error: function ( xhr ) {
                        alert( "error" );
                    }
                });
            }
        </script>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    <form>
        <div class="row">
            <div class="row">
                <div class="col-lg-12 form-group">
                    <label for="email">Email</label>
                    <input
                            type="email"
                            id="email"
                            class="form-control"
                            name="email"
                            required
                            pattern="'^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$'"
                    >
                </div>
                <div class="row moveRight">
                    <div class="col-lg-12 form-group">
                        <label for="firstname">First Name</label>
                        <input
                                type="text"
                                id="firstname"
                                class="form-control"
                                name="firstname"
                                required
                        >
                    </div>
                </div>
                <div class="row moveRight">
                    <div class="col-lg-12 form-group">
                        <label for="lastname">Last Name</label>
                        <input
                                type="text"
                                id="lastname"
                                class="form-control"
                                name="lastname"
                                required
                        >
                    </div>
                </div>
                <div class="row">
                    <div class="col-lg-12">
                        <button onclick="myAjax()" class="btn btn-success" id="startnode" type="submit">Submit</button>
                    </div>
                </div>
            </div>
        </div>
    </form>
    <?php include 'index.php';?>
    </body>
    </html>

php file

<?php

function bb() {
   //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 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/hubid/guid';

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

bb();

?>
8
  • @Ivar my mistake. I fixed it in the wording of my question. Any ideas on a solution? Commented Aug 26, 2017 at 0:02
  • 1
    Well, you say you are trying to post a form, yet you don't add any data to the ajax request (data : { }). You are getting a 204 which is the status code for a successful request that has no response body, but you don't show the part that returns the request/status code. Without that information it's hard to say. Commented Aug 26, 2017 at 0:12
  • Why are you including index.php in the main file? Commented Aug 26, 2017 at 0:27
  • maybe if you removed the @ on your statements, curl would have something to say as to why there is not payload on the response. Commented Aug 26, 2017 at 0:27
  • @YvesLeBorg I removed the @ on my statements and the response of 204 is still thrown. Commented Aug 26, 2017 at 0:40

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.