I tried to do a small thing using Ajax. I just try to replace a div.
This is what I have done so far.
I created a admin menu using add_menu_page
.
Then I add a form to that page with a text field. I just need to replace a div when click the submit button. But it did not works. When I check console in browser it says Uncaught TypeError: Cannot read property 'ajax' of undefined
function myajaxfunction() {
$.ajax({ //ajax request
url: ajaxurl,
data: {
'action':'rs_like_request',
'post_type' : $('#post_type').val() // value of text box having id "post_type"
},
success:function(data) { //result
$(".showdiv").html(data); //showdiv is the class of the div where we want to show the results
},
error: function(errorThrown){
console.log(errorThrown);
}
});
}
<form action="" method="post">
<label>Write post type : </label>
<input type="text" name="post_type" id="post_type" value=""/>
<input type="button" value="Show Post Titles" onclick="myajaxfunction()"/>
<div class="showdiv"></div>
</form>
function rs_like_count() {
if ( isset($_REQUEST) ) { // $_REQUEST is having all the data sent using ajax
$post_type= $_REQUEST['post_type'];
echo $post_type;
}
die();
}
add_action( 'wp_ajax_rs_like_request', 'rs_like_count' );