WordPress ajax request not getting value by PHP

Viewed 65

I sent an ajax request by a button, ajax requests work perfectly also responding. I checked the browser Network it is fine. My problem is I am not getting value by PHP request. Here is my code given below:

function my_enqueue(){
        wp_enqueue_script( 'ajax-script', plugin_dir_url( __FILE__ )  . '/assets/js/awqvCustom.js', array('jquery') );
        wp_localize_script( 'ajax-script', 'my_ajax_object',   array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
    }
    add_action( 'wp_enqueue_scripts', 'my_enqueue' );

    /* Custom Ajax */
    function data_custom_ajax(){
        if ( isset($_POST)) {
            $id = $_POST['id'];
            echo $id;
        }
      die();
    }
    add_action('wp_ajax_nopriv_data_custom_ajax', 'data_custom_ajax');
    add_action('wp_ajax_data_custom_ajax', 'data_custom_ajax');
    
    /* Modal Markup*/
    
    function modalBody(){?>
        <div class="my-modal">
            <div id="ajax_result"></div> //getting ajax success value here
            <?php 
                $getId = (isset($_POST['id']) ? $_POST['id'] : '');
                echo $getId; //nothing get
             ?>
        </div>
        
    <?php }
    add_action('get_footer', 'modalBody');

jQuery ajax code:

jQuery(document).ready(function () {
    'use strict';
    jQuery(".btn-container").on('click','.open-modal', function(e){
        e.preventDefault();
        var id =  jQuery(this).data('id');
        
        jQuery.ajax({
                type: "POST",
                url: my_ajax_object.ajax_url,
                dataType:"json",
                data: { 
                    action: 'data_custom_ajax',
                    id: id,
                },
                cache: false,
                success: function(data){                   
                    if(data !=''){
                       jQuery('#ajax_result').html(data); 
                       var modal = jQuery(".my-modal").wgModal();
                       modal.openModal();                    
                    }
                }
        });
    });

I checked with the debugger. The ajax variables seem to be sent

I checked with the debugger. The ajax variables seem to be sent

0 Answers
Related