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();

?>
  • @Ivar my mistake. I fixed it in the wording of my question. Any ideas on a solution? – Perry Livingston Aug 26 '17 at 00: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. – Ivar Aug 26 '17 at 00:12
  • Why are you including index.php in the main file? – Mohammed Akhtar Zuberi Aug 26 '17 at 00: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. – YvesLeBorg Aug 26 '17 at 00:27
  • @YvesLeBorg I removed the ```@``` on my statements and the response of 204 is still thrown. – Perry Livingston Aug 26 '17 at 00:40
  • @MohammedAkhtarZuberi I thought i needed to make a formal connection between the html file and php like js or css – Perry Livingston Aug 26 '17 at 00:40
  • Follow simple logic step-by-step. 1. Ensure that you have a proper Action script. This script is not required to be part of your HTML. 2. Ensure that you have proper data being posted from your HTML form. This is as @Ivar also mentioned. If you need help in testing this, let me know. 3. Ensure that you are getting a proper response, even if it is a static echo, from your PHP file. In case you need help, let me know. 4. Display the result in your HTML to ensure that the transaction is complete. Let me know in case you need help. – Mohammed Akhtar Zuberi Aug 26 '17 at 00:46
  • @MohammedAkhtarZuberi I'm definitely going to need some help with this. This is the first time i'm using both ajax and php – Perry Livingston Aug 26 '17 at 01:03

0 Answers0