简体   繁体   中英

Passing form search submit data to redirected results page

I have a search page that uses an AJAX (WordPress) form to filter results. Let's say results.php for the example.

What I would like to do is use the same form on another page home.php but upon submit will redirect to the results.php search page with it's results (into #response ). What would be the best way to pass and redirect form data to the AJAX results page?

AJAX FORM

$('#filter').submit(function(e){
    e.preventDefault();
    var filter = $('#filter');

    $.ajax({
        url: filter.attr('action'),
        data:filter.serialize(),
        type: 'POST', // POST
        dataType: 'json',
        success: function(response) {   
        $('#response').html(response);
        }
    });
    return false;

});

FORM

<form action="<?php echo site_url() ?>/wp-admin/admin-ajax.php"     method="POST" id="filter">

   <input id="address" name="property_location" placeholder="Input Address" type="text"/>

   <button type="submit"  id="filter">Search</button>

   <input type="hidden" name="action" value="my_search"> //my_search taken from ajax function query.

</form>

If you're redirecting the page when the form is submitted, then there's no need to pass values back to home.php and then redirect.

You can still use AJAX to submit but do the redirect from the PHP side. Keep the AJAX though in case there is an error you need to inform the user about.

So in the PHP file you're submitting to, don't echo anything back, just redirect. If there's an error, then send back whatever is appropriate for the error message.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM