I am trying to send data from Ajax to Php File

Viewed 60

Ajax Code

jQuery(document).ready(function($){
$("#login_otp_btn").click(function(event){
    val();
  //event.preventDefault();
  jQuery.ajax({
            url: wpac_ajax_url.ajax_url,
            type:'POST',
            data :{
                action : 'login_ajax_callback',
                login : $("#login_mobile").val(),
            },
            success:function(data)
            {
                alert(data + "Successfull.........." );
                     }
            });
           $("#login_otp_field").show();  

      
})

});

Php Script To add action hook...

add_action('wp_ajax_login_ajax_callback',array($this,'login_ajax_callback'));
add_action('wp_ajax_nopriv_login_ajax_callback',array($this,'login_ajax_callback'));

Php script to localize....

wp_localize_script( 'ajax', 
            'wpac_ajax_url', 
            array( 'ajax_url'    => admin_url( 'admin-ajax.php' ))

Hook Callback...

function login_ajax_callback(){

            $ajax_login = $_POST["login"];
            
            
                $user_creds = ($this->getWp_users());   
                    $user_id =  $user_creds->user_id;
                    //var_dump($user_id);
                    //echo $user_id;
                    
                    //echo($user_id) . '<br>' ;
                     /*foreach($user_creds as $u_data){
                          echo $u_data .'<br>';
                        }*/
        
                    wc()->session->set("bypass",$user_id);
        
                    $login_val = $this->login_check();   
                    if(empty($_POST[$ajax_login])){
                        wc_add_notice("Empty phone number field", "error");
                    } 
                    elseif(strlen($_POST[$ajax_login])<11)
                    {
                        wc_add_notice("Invalid number","error");
                    }
                        elseif($login_val == true)
                        {       
                                $login_api_status = $this->login_sms_api();
                                if($login_api_status == "ACCEPTED"){
                                    wc_add_notice("OTP has been sent to your mobile Enter Otp Below ", "success");
        
                                }else{
                                    wc_add_notice("Something Went Wrong With The SMS Gateway","error");
                                }
                        }   
                            else
                            {
                                wc_add_notice("Number Not Registered please proceed to registeration","error");
                            }
        
    

I am unable to send the login property of data to my php script I dont know why it is not getting sent to the php file.I have searched it many times but unable to find a solution.Help will be highly appreciated

0 Answers
Related