我有一个表单,它将post数据提交给PHP函数search,search函数查询我添加到WordPress数据库的表。提交后,我想重定向到我在仪表板中添加的结果页面。到目前为止,我只能使用Ajax重定向,但我想从PHP文件重定向。
AJAX
$(\'#finderForm\').on(\'submit\', function(e) {
//e.preventDefault();
var form = $(\'#finderForm\').serialize();
$.ajax({
type: "POST",
url: "wp-admin/admin-ajax.php?action=search",
data: form,
success: function (data) {
console.log(\'Submission was successful.\');
console.log(data);
console.log(window.location.href);
//window.location.href = \'?page_id=7=\' + data;
},
error: function (data) {
console.log(\'An error occurred.\');
console.log(data);
},
});
});
我在PHP文件中的搜索函数,我想从这里重定向: add_action("wp_ajax_search", "search");
function search() {
$name = $_POST[\'name\'];
$mydb = new wpdb(\'root\',\'\',\'i3264185_wp1\',\'localhost\');
$query = "SELECT event_name FROM event WHERE event_name LIKE \'$name%\'";
$result = $mydb->get_results($query);
$result = json_encode($result);
wp_redirect( home_url(\'/?page_id=7\') );
exit;
echo $result;
die();
}