I have a situation where I've to send Data from frontend without using any Plugin Due to Code is loading on the same page using Ajax so I can't use any plugin Because Plugin files will load on refreshing of the file but the page is not loading.
I am using post snippet plugin to sending PHP and JS to the page which is loading on the page. PHP Code is:-
if ( is_user_logged_in() ) {
global $current_user;
$unit_id= get_the_ID();
$userid = $current_user->ID;
global $wpdb;
$results = $wpdb->get_row( "SELECT * FROM wp_prepze_video_limits WHERE user_id =$userid && unit_id = $unit_id");
$counter = $results->counter;
if($counter!=0)
{
echo $counter;
}
else
{
$myObj->counter = $counter;
$myObj->unit = $unit_id;
$myObj->user = $userid;
$myJSON = json_encode($myObj);
echo $myJSON;
}
}
JS CODE is:-
$('.clickable').on('click', function(){
var counter = [uname][/uname]; //This is post snippet Name that outputs Above PHP Code in here
var user_counter;
if(counter.counter==null)
{
user_counter = 0;
}
//var user_counter= counter.counter;
var unit_id = counter.unit;
var user = counter.user;
var ajax= counter.ajax_url ;
jQuery.ajax({
url : AjaxUrl,// I'm Not Sure Where do I Send the Request
type : 'post',
data : {
action : 'post_love_add_love',
unit_id : unit_id,
user_counter : user_counter,
user : user,
},
success : function( response ) {
//jQuery('#love-count').html( response );
console.log(response);
}
});
});
I am not sure about where do, I send this request.
post_love_add_love
and add add ajax actionadd_action('wp_ajax_post_love_add_love','post_love_add_love'); add_action('wp_ajax_nopriv_post_love_add_love,'post_love_add_love';
url : AjaxUrl
in the AJax Request from the Scriptwp_enqueue_script('ur_js_id',get_template_directory_uri().'/js/your.js');
wp_localize_script('ur_js_id','ajax_object',array('ajax_url'=>admin_url('admin-ajax.php')));
----------------------url : ajax_object.ajax_url,
---------------------------------add_action('wp_ajax_post_love_add_love','post_love_add_love');
add_action('wp_ajax_nopriv_post_love_add_love,'post_love_add_love');
function post_love_add_love(){ 'your code' }