I'd like to get a function as Json response in my Ajax call, after the document is ready. I'm not sure if I'm on the right way but here is what I did so far:
My Html
<!--Where i want to load my function-->
<div id="applications"></div>
My Ajax/Jquery
jQuery(document).ready(function(){
init_applications();
});
function init_applications(){
var app_data = new FormData();
app_data.append('action', 'applications');
app_data.append('uid', uid);
app_data.append('pid', pid);
jQuery.ajax({
method: 'post',
url: ajaxurl,
dataType: 'json',
data: app_data,
processData: false,
contentType: false,
beforeSend:function(data){
//something
},
success:function(data) {
$('#applications').html(data.htmlapp);
//console.log(data);
},
error: function(data){
//console.log(data);
}
});
//alert("a");
}
Of course in my functions.php
add_action('wp_ajax_applications', 'applications');
function applications(){
$uid = $_POST['uid'];
$pid = $_POST['pid'];
$applications = inside_applications();
$response = array('htmlapp'=>$applications);
wp_send_json( $response );
}
and Finally I've created my separate function always in functions.php
function inside_applications(){
//Some html code and instructions
}
Is this possible or I'm totally out of the way? Can you please give me some directions eventually? Many thanks.