Search code examples
phpjquerywordpressgravityforms

Use the same gform twice on the same page


I'm working in a website that needs to have the same form displayed multiple times on the same page.

The plugin that builds the forms is Gravity Forms.

I have already research on the documentation provided and I couldn't find any action that does the job.

I think I'll need to create a function that store in an array the ID of every single form created and do a comparison for every new form, so then I can probably change the ID of the form element(???).

Any guesses?


Solution

  • Solved the problem by printing the GravityForm inside a noscript tag and removing from that when the form is required.

    See the example:

    var $formContainer = $(".formContainer");
    function hide_form($container) {
      $container.slideUp();
      var add_noscript_tag = "<noscript>" + $container[0].innerHTML + "</noscript>";
      $container.empty();
      $container.append(add_noscript_tag);
    }
    function show_form($container) {
      var remove_noscript_tag = $container[0].innerHTML
        .replace("<noscript>", "")
        .replace("</noscript>", "");
      $container.empty();
      $container.append(remove_noscript_tag);
      $container.slideDown();
    }
    $("button").click(function() {
      var $FormContainer = $(this).next();
      if ($FormContainer.is(":visible")) {
        hide_form($FormContainer);
      } else {
        var $otherForm = $(".formContainer:visible");
        if ($otherForm.length > 0) {
          hide_form($otherForm);
        }
        show_form($FormContainer);
      }
    });
    button {
      margin: 20px 0;
    }
    .formContainer {
      display: none;
      background: #eee;
    }
    .formContainer form {
      padding: 20px;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div>
      <button>open form 1</button>
      <div class="formContainer">
        <noscript>
          <form>
            <input type="text" value="Name"/>
            <input type="text" value="Phone"/>
            <input type="text" value="Email"/>
          </form>
        </noscript>
      </div>
    </div>
    <div>
      <button>open form 2</button>
      <div class="formContainer">
        <noscript>
          <form>
            <input type="text" value="Name"/>
            <input type="text" value="Phone"/>
            <input type="text" value="Email"/>
          </form>
        </noscript>
      </div>
    </div>