0

I have a Hubspot (service outside my server) form that appears after a certain amount of pages is viewed and want to set a cookie after that form is submitted. I've tested the code with an HTML form and it worked fine, but with the Javscript created form the PHP isn't seeing that it was submitted.

Hubspot Form

<div id="access" class="modalWindow">
    <div>
        <h1>To access the rest of this eBook, please fill out the following form.</h1>
        <script charset="utf-8" src="//js.hsforms.net/forms/current.js"></script>
        <script>
          hbspt.forms.create({ 
            portalId: '268874',
            formId: '2f79f36a-5c90-47e3-bd54-9d5f0fc674d1'
          });
        </script>
    </div>
</div>

PHP if Statement

<?php
    if (!empty($_POST)) {
        $value = "Ebook";

        setcookie("ebookAccess", $value, time()+3600*24*360, "/ebooks/", "mytestserver.com");

        header("Refresh:0");
    }?>
  • JavaScript isn't submitting the form or PHP doesn't see the form submitted information from the modal? – Biotox Jan 22 '14 at 16:21
  • The form is being submitted to Hubspot so the PHP isn't seeing any of the information, thus not noticing it's been submitted, so I'm looking for a way to make it notice. – deliciouscheese Jan 22 '14 at 16:23
  • That's because the form action doesn't sound like it's going to PHP on your server. So, there is no `$_POST`. You can always add a `click` listener to the form submit button, set a cookie in JavaScript and refresh the page. – Biotox Jan 22 '14 at 16:25
  • Sorry, Javascript is not my forte. Could you explain further on how to do that? – deliciouscheese Jan 22 '14 at 16:39
  • We would need to see a sample of the form to know what sort of classes or ids are available. -- The actual HTML produced. – Biotox Jan 22 '14 at 16:51
  • The form code is from an outside service (Hubspot.com). This is the closest I have to any output available - http://js.hsforms.net/forms/current.js – deliciouscheese Jan 22 '14 at 16:57

1 Answers1

0

Javascript is client side. php is server side. you have two options add a click event or user ajax to make server side post. that way php will pick it up.

    <form action="/path.to.your.php.file.php" method="POST" onSubmit="return validate();">
 ... your fields...
 <input type="submit" value="validate and submit"/>
</form>

<script>
  function validate(){
    // do your validation here
    return true; // return true if validation was ok, false otherwise
  }
</script>

this is the simplest way i can put it.

Shahin Khani
  • 199
  • 4
  • 14