将表单搜索提交数据传递到重定向结果页面

时间:2017-07-11 20:47:34

标签: javascript php jquery ajax wordpress

我有一个搜索页面,它使用AJAX(WordPress)表单来过滤结果。让我们举例说明results.php

我想要做的是在另一个页面home.php上使用相同的表单,但提交后会重定向到results.php搜索页面及其结果(进入#response )。将表单数据传递并重定向到AJAX结果页面的最佳方法是什么?

AJAX表格

$('#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>

1 个答案:

答案 0 :(得分:1)

如果您在提交表单时重定向页面,则无需将值传递回home.php然后重定向。

你仍然可以使用AJAX提交,但是从PHP端进行重定向。保留AJAX,以防万一需要通知用户有错误。

因此,在您提交的PHP文件中,不要回复任何内容,只需重定向即可。如果出现错误,则发送适用于错误消息的任何内容。