0

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.

3
  • You have to make a function named post_love_add_love and add add ajax action 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';
    – DHL17
    Commented Jan 19, 2018 at 13:20
  • Where do I put this code, What should be the url : AjaxUrl in the AJax Request from the Script Commented Jan 19, 2018 at 14:31
  • wp_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' }
    – DHL17
    Commented Jan 22, 2018 at 4:40

1 Answer 1

0
jQuery.ajax({
                    url : postlove.ajax_url,
                    type : 'post',
                    data : {
                        action : 'post_love_add_love',
                        unit_id : unit,
                        user_counter : new_counter,
                        user : user,

                    },
                    success : function( response ) {
                        //jQuery('#love-count').html( response );
                        console.log(response);
                    }
                });

This Works for me. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.