Storing session variable without interfering with Varnish

Question

I have a form (www.example.com/form) that submits to a 3rd party app to add users to a contact list. The vendor does not provide any means of using AJAX, rather they allow you to specify a URL to redirect to after the form has processed. In an effort to have the UX of using this archaic form come as possible to functioning like an AJAX-enabled form, I am specifying the same page the form is on as the redirect page. I need to make it such that if the user filled out the form, a confirmation page will be displayed and if not, the form itself is displayed.

I am currently adding a query string parameter to the redirect URL (through the 3rd party app), so it looks something like www.exampe.com/form/?form_success=1. This way is easy enough to show the appropriate content (form or confirmation), but i don’t like the fact that someone could go to www.exampe.com/form/?form_success=1 without filling out the form.

I had the idea of making an AJAX call onSubmit that would do something to the effect of:

// JavaScript

$('form').submit(function() {     
   $.ajax({
      url:  admin-ajax.php
      method: 'post',
      action: 'handle_ajax_submit'
    },
    success: function() {
      return true;
    });
    return false;
});



// PHP

function handle_ajax_submit() {
  if (!session_id()) {
    session_start();
  }
  $_SESSION['form_submitted'] = true;
}

I’d then use that value to determine which piece of content is displayed, form or confirmation. The problem with this is that my understanding is that the use of _SESSION prevents Varnish from caching the page, which i’d obviously like to avoid if possible.

Is there another”standard” way to handle this kind of situation in WordPress specifically? If not, would a standard cookie be the way to go here… or prhaps local storage? Both would require that AJAX call onSubmit, correct?

0
Daveh0 4 months 0 Answers 12 views 0

Leave an answer