Reputation: 325
I have drop-down element, and on selection of one item I need to query from database and to show me result in second drop-down.
I have several drop-down, I need to narrow the filter.
I have to do it with ajax, but I am new in this. Where should i write query statement and which path to put in URL
because I am doing it in WP, do I always have to do use url: admin-ajax.php default in WP?
//dropDownKlix ajax call
jQuery("#dropDownKlix").change(function(){
jQuery.ajax({
url: 'admin-ajax.php',
data: {"name":"name","value":$("#dropDownKlix option:selected").val()},
type: 'POST',
dataType: "json",
success: function(data) {
alert('Success!');
}
});
});
this is my ajax request, but I dont know where to put sql statement to filter and retrive data to show in another drop-down.
Should I put in callback query or ?
If you need more information, please ask me.
Upvotes: 0
Views: 1600
Reputation: 10240
this is my ajax request, but I dont know where to put sql statement to filter and retrive data to show in another drop-down.
You should use the wp_ajax_{action}
hook to create a custom handler for your AJAX request.
In your AJAX request, you should set the action property to {action}
. For example:
AJAX request example:
jQuery.ajax({
type: 'POST',
url: 'admin-ajax.php',
dataType: 'json',
data: {
'action' : 'your_action',
'name' : 'name',
'value' : $( '#dropDownKlix option:selected' ).val()
},
success:function( data ) { ...
Custom handler example:
function my_handler() {
// Handle the request then generate a response.
}
add_action( 'wp_ajax_your_action', 'my_handler' );
If you want to handle requests from unauthorised users on the front-end, then use wp_ajax_nopriv_{action}
For more detailed info, see the AJAX in Plugins article.
Refs:
Upvotes: 2