Skip to content

Instantly share code, notes, and snippets.

@kylephillips
Created December 20, 2016 18:05
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kylephillips/3c0e3decc3dd952f214f23c899a83c9e to your computer and use it in GitHub Desktop.
Save kylephillips/3c0e3decc3dd952f214f23c899a83c9e to your computer and use it in GitHub Desktop.
Populate and submit a simple locator form dynamically and automatically on page load, using a GET parameter
<?php
/**
* Using this method, a GET parameter can be passed to the page, enabling a simple locator form to be populated and submitted dynamically.
* One use case may exist where the user submits a gravity form, and is redirected to a list of locations without needing to resubmit the simple locator form.
* This code would be placed in a page template.
*/
// First, we check if a GET parameter named "search" exists. If so, we sanitize it and set it for our use
// This could be passed via a link, or through another form submission
$location_search = ( isset($_GET['search']) ) ? sanitize_text_field($_GET['search']) : false;
// If the variable is set, we perform some simple jquery to populate and submit the form on page load
if ( $location_search ) :
?>
<script>
$(window).load(function(){
$('.wpsl-search-form').val("<?php echo $location_search; ?>");
$('.wpslsubmit').trigger('click');
});
</script>
<?php
endif;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment