Forms API Not Working - 400 Error


#1

The forms API is not working and this is business critical for us. The form is available on the page http://www.tothenew.com/contact-us page.

The website is on Drupal and using Forms API. We have tested the end URL (
https://forms.hubspot.com/uploads/form/v2/481864/056229e6-d64e-41ff-9483-3f991d84df2d) using both http:// and https://

Below is the PHP code:

<?php namespace Drupal\custom\Form; use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormStateInterface; use Symfony\Component\HttpFoundation\Request; use \Symfony\Component\HttpFoundation\RedirectResponse; /** * Contribute form. */ class RequestForServices extends FormBase { /** * {@inheritdoc} */ public function getFormId() { return 'request_for_services_form'; } /** * {@inheritdoc} * * Implements buildform. * * @param array $form * From render array. * @param \Drupal\Core\Form\FormStateInterface $form_state * Current state of form. */ public function buildForm(array $form, FormStateInterface $form_state) { $form['#prefix'] = ''; $current_path = \Drupal::service('path.current')->getPath(); if ($current_path == '/node/75') { $form['#prefix'] = '
Whether you\'re searching for a new partner or a new career, we want to hear from you! Fill out the below form, and one of our team members will contact you shortly.
'; } $form['fname'] = array( '#title' => $this->t('First Name'), '#prefix' => '
', '#type' => 'textfield', ); $form['lname'] = array( '#title' => $this->t('Last Name'), '#type' => 'textfield', '#suffix' => '
', ); $form['email'] = array( '#title' => $this->t('Email'), '#type' => 'email', ); $form['phone'] = array( '#title' => $this->t('Phone'), '#type' => 'tel', ); $form['company'] = array( '#title' => $this->t('Company'), '#type' => 'textfield', ); $form['requirement'] = array( '#title' => $this->t('Requirement'), '#type' => 'textarea', ); $form['submit'] = array( '#type' => 'submit', '#value' => t('Talk to Us'), ); return $form; } /** * {@inheritdoc} */ public function validateForm(array &$form, FormStateInterface $form_state) { $fname = $form_state->getValue('fname'); if ($fname == '') { $form_state->setErrorByName('fname', t('Please enter the First Name.')); } $lname = $form_state->getValue('lname'); if ($lname == '') { $form_state->setErrorByName('lname', t('Please enter the Last Name.')); } $email = $form_state->getValue('email'); if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { $form_state->setErrorByName('email', t('Please enter the email.')); } $phone = $form_state->getValue('phone'); if (filter_var($phone, FILTER_VALIDATE_INT) === false) { $form_state->setErrorByName('phone', t('Please enter the phone number.')); } $company = $form_state->getValue('company'); if ($company == '') { $form_state->setErrorByName('company', t('Please enter the company name.')); } $requirement = $form_state->getValue('requirement'); if ($requirement == '') { $form_state->setErrorByName('requirement', t('Please enter the requirement.')); } } /** * {@inheritdoc} * * Implements submit callback. * * @param array $form * From render array. * @param \Drupal\Core\Form\FormStateInterface $form_state * Current state of form. */ public function submitForm(array &$form, FormStateInterface $form_state) { global $base_url; $fname = $form_state->getValue('fname'); $lname = $form_state->getValue('lname'); $email = $form_state->getValue('email'); $phone = $form_state->getValue('phone'); $company = $form_state->getValue('company'); $requirement = $form_state->getValue('requirement'); $hubspotutk = $_COOKIE['hubspotutk']; $ip_addr = $_SERVER['REMOTE_ADDR']; $jobtype = 'Sales'; $pageTitle = 'Contact us'; $uri = $_SERVER['REQUEST_URI']; if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR']) { $clientIpAddress = $_SERVER['HTTP_X_FORWARDED_FOR']; } else { $clientIpAddress = $_SERVER['REMOTE_ADDR']; } $hubspot_context = array( 'hutk' => $hubspotutk, 'ipAddress' => $clientIpAddress, 'pageUrl' => 'http://www.tothenew.com/contact-us', 'pageName' => $pageTitle ); $hubspot_string = json_encode($hubspot_context); $str_post = "first_name=" . urlencode($fname) . "&last_name=" . urlencode($lname) . "&message=" . urlencode($requirement) . "&email=" . urlencode($email) . "&phone=" . urlencode($phone) . "&jobtype=" . urlencode($jobtype) . "&company=" . urlencode($company) . "&hs_context=" . urlencode($hubspot_string); $endpoint = 'https://forms.hubspot.com/uploads/form/v2/481864/056229e6-d64e-41ff-9483-3f991d84df2d'; $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); @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 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); //var_dump($status_code);exit; if ($status_code == '200') { global $base_url; $response = new RedirectResponse($base_url . '/thanks-form'); $response->send(); } else { drupal_set_message('Data for this email already exist.', 'error'); /* global $base_url; $response = new RedirectResponse($base_url . '/contact-us?q=request-form'); $response->send(); */ } } } ?>

#2

Hi @Ankit_Gupta1

I’m not seeing any problems with the POST URL you have, and was able to submit a test contact using that URL. Are you getting any response from HubSpot at all? Including any error codes?

The POST URL will return that error in a web browser, since the browser would only make a GET request, and the form submission URLs only support POST requests.