Save Search System

I am trying to create a save search notification system whereby a user can search certain parameters on my site and save the search query like Ebay does or other classified ad websites do. I understand the save search side of things, I have created a database table that contains the search_id, the search_name, the search_parameter and the user_id (Please let me know if this the correct method to go about doing this. I’m open to new ideas..) Anyway, I understand that side of things to a certain degree but the side I don’t understand how it works is the notification side of things, how do I begin to check if new posts have been added that match that certain query and how do I notify a user once that has happened? Any advice and possible coding help would be greatly appreciated! (I’m kind of a coding newbie.)

Btw, I still don’t know how to fetch the search parameter as it doesn’t sow up in the url it’s kind of done behind the scenes by ajax. So any help regarding that would also be appreciated.

Here is the code that I have so far:

The PHP:

function save_this_search() {
global $wpdb;

$my_user_id = get_current_user_id();
$search_name = $_REQUEST[‘search_name’];
$search_parameter = $_GET[‘search_parameter’];

$count = $wpdb->get_var( “SELECT COUNT(*) FROM search_queries” );

$id = $count + 1;

$wpdb->insert(
‘search_queries’,
array(
‘search_id’ => $id,
‘search_name’ => $search_name,
‘search_parameter’ => ‘test’,
‘user_id’ => $my_user_id,

),
array( ‘%s’, ‘%s’, ‘%s’, ‘%s’ )
);

/*echo ‘The search name: ‘. $search_name;
echo ‘My user ID: ‘. $my_user_id;
echo ‘Id: ‘. $id;*/

die();
}
add_action(‘wp_ajax_save_this_search’, ‘save_this_search’);
add_action(‘wp_ajax_nopriv_save_this_search’, ‘save_this_search’);

The Jquery AJAX:

$(‘.save_search_form’).submit(function(e) {
e.preventDefault();

var search_name = $(‘.save-search-input’).val();

$.ajax({
type: “POST”,
url: ajaxurl,
data: {
action : ‘save_this_search’,
search_name: search_name
},
beforeSend: function() {
$(‘save-search-input’).prop(‘disabled’, true);
},
success: function(data) {
console.log(data);
$(‘save-search-input’).prop(‘disabled’, false);
//$(‘#savesearchmodal .modal-body’).text(‘Search saved successfully! You will be notified when new ads are created matching your search criteria.’);
}
});
});

And finally the HTML:

<form class=”save_search_form”>
<input class=”save-search-input” placeholder=”Name this search” type=”text” name=”save_search_input” />
<button type=”submit” class=”save-search-submit-button”>Save</button>
</form>

Read more here:: Save Search System

Leave a Reply

Your email address will not be published. Required fields are marked *