I have checked so many links but i didn't succeeded using ajax in wordpress.
Foooter.php
<script type="text/javascript">
var ajaxurl = "<?php echo admin_url('admin-ajax.php'); ?>";
</script>
custom.js
jQuery('#elementid').on('keyup', jqueryfunction);
function jqueryfunction(id) {
var value = jQuery(this).val();
if (value.length > 3) {
jQuery.ajax({
url: ajaxurl,
type: 'POST',
data: 'value=' + value,
success: function (data) {
console.log(data);
}
});
} else {
$('.showcount').remove();
}
}
functions.php
// I think i am wrong here. I don't know where to write this code
wp_enqueue_script('jquery');
function mycustomfunction() {
echo "Function found";
}
add_action('wp_ajax_mycustomfunction', 'mycustomfunction');
add_action('wp_ajax_nopriv_mycustomfunction', 'mycustomfunction');
In the console it only shows 0 but it should show Function found. Can someone please refer me where to write this code so that it will work.