I have a form that submits post data to PHP function called search, the search function queries a table I added to the WordPress database. After the submit I would like to redirect to a results page that I added in the dashboard. So far I can only redirect using Ajax, but I would like to redirect from the PHP file.
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);
},
});
});
My Search Function in PHP File, I want to redirect from here:
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();
}