Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

My form submission get blocked because the frame is sandboxed and the 'allow-forms' permission is not set

I'm trying to build a custom form and submission post for Hubspot.

I have the following code

HTML

<head>
<script src="prezzi-form-submit.js"></script>
</head>
<body>

  <form class='form-inline' id='my-custom-form'>
    <div class="form-group">
      <input type='email' class='form-control' placeholder='Your email address' required>
    </div>
    <button class="btn btn-primary" type='submit'>Sign up</button>
  </form>

  <!-- Actual form that gets submitted to HubSpot -->
  <div class="hidden" id='hubspot-form'>
    <script charset="utf-8" src="//js.hsforms.net/forms/current.js"></script>
    <script>
      hbspt.forms.create({
        portalId: 'my-portal-id',
        formId: '92b9b82a-0da2-4e23-8a30-04541c05ce6d',
        onFormReady: function($form) {
          $form.attr('target', 'hubspot-iframe');
        }
      });
    </script>

    <!-- iFrame that data will get submitted to. This hack stops the page redirect. -->
    <iframe name="hubspot-iframe" id="hubspot-iframe" sandbox="allow-forms"></iframe>
  </div>
</body>

JS (prezzi-form-submit.js)

// // Send form data to HubSpot from the client.
function submitToHubSpot(data) {
  var $form = $('#hubspot-form form'),
      k;

  // Loop through each value and find a matching input.
  // NOTE: Doesn't support checkbox/radio.
  for (k in data) {
    $form.find("input[name='" + k + "']").val(data[k]);
  }

  $("form input:submit").trigger("click");
}

// Here's how you'd use this.
$('#my-custom-form').on('submit', function() {
  var formData = {};
  $(this).serializeArray().forEach(function(data) {
    formData[data.name] = data.value;
  });

  submitToHubSpot(formData);

  // We sent the data. Now do whatever else you want.
  alert('Gee, thanks Jonathan! Now I can focus on onboarding my customers with Appcues!');
  window.location.href = 'http://appcues.com';
})

When I press the submit button, I get the following error in the console

Blocked form submission to " " because the form's frame is sandboxed and the 'allow-forms' permission is not set.

As you can see I have the

sandbox="allow-forms"

set in the I frame but it isn't working. How can I fix this error?

like image 849
Matteo Boscolo Avatar asked May 16 '18 09:05

Matteo Boscolo


2 Answers

Sometimes when you click a link from an application, the tab opened will have javascript disabled/sandboxed.

Close the tab and reopen the same URL in a fresh tab, it might work.

like image 127
Benoit Duffez Avatar answered Sep 20 '22 11:09

Benoit Duffez


Ran into the same problem with an iFrame form on Hubspot and got the same JS error. Discovered it has to do with the live preview using the HS Design tool.

In the drop down at the top there's the "Live preview with display options" then the "Preview without display options". It's the "preview with display options" selection that makes it "Sandboxed", try the one without. Hope this is helpful for someone.

like image 38
Suzisqueue Avatar answered Sep 17 '22 11:09

Suzisqueue