0

I keep looking my error , but I can't find it. My php and js script works fine 5 month ago when I let them to dedicate myself to design Normally this ajax query must be launch from my plugin ( and works fine before) , but to try to resolve my problem I relocate them to template without change.

Could you please help me to understand where is located my error ? I have always Error 500 and I can't have any errors to help me to resolve .

Form :

<div id="FormLogin" >
<form id="LogAtv" method="post" action="atv_user_login" >
<p> Id User</p>
    <input id='IdUser' name="user_login" type="text" required="required" autocomplete="username">
<p> Password</p>
    <input id="PasswordUser" type="password" name="user_pass" autocomplete="new-password" required="required">
    
    <input type="submit" name="Atvsubmit" id="btn_Login" value="Valider &#xf058">
    <p class="status"></p>
</form>

PhP:

`function atv_user_admin() {
wp_enqueue_script('script', get_template_directory_uri() . '/script/test.js', array('jquery'), '1.0', true);
wp_localize_script('script', 'wpAjax', array('ajaxUrl' => admin_url('admin-ajax.php')));
}
add_action('login_enqueue_scripts', 'atv_user_admin');
add_action('wp_enqueue_scripts', 'atv_user_admin');
add_action('wp_ajax_atv_user_login','handle_user_login');
add_action('wp_ajax_nopriv_atv_user_login', 'handle_user_login');

function handle_user_login(){
    
    $log_param = isset($_REQUEST['log-param'])?trim($_REQUEST['log-param']):"";
    if($log_param == "login_data" ){
        $info = array();
        $info['user_login'] = $_POST[user_login];
        $info['user_password'] = $_POST[user_pass];
        $info['remember'] = true;
        //$isActivated = get_user_meta($info->ID, 'is_activated', true);
        $user_signon = wp_signon($info, false);
        
        if(is_wp_error($user_signon)){
            $error_string = $user_signon->get_error_message();
            echo json_encode(array('status' => 0));
            echo '<div id="message" class="error"><p>' . $error_string . '</p></div>';
        }else{
            if($isActivated !== 1){
                echo json_encode(array('useremailauth' =>'Description text'));

            }else{
                echo json_encode(array('status' => 1));
                wp_clear_auth_cookie();
                wp_set_auth_cookie( $info->ID, true, is_ssl() );
                wp_user_settings($info->ID);
                setcookie('useratv','connected', time() + (3600), "/"); 
                wp_set_current_user($info->ID, $username);
                
                if(is_user_logged_in()){
                    $current_user = wp_get_current_user();
                    $userpicture = "test";
                    echo json_encode(array('userpicture' => $userpicture));
                }
            }
        }
    }
wp_die();
}`

jquery:ajax (test.js)

$(document).ready(function() {
    
    $(function(){
        $('#btn_Login').on('click', function(e){
            e.preventDefault();
            var formdata = $('#LogAtv').serialize();
            formdata += '&action=atv_user_login&log-param=login_data';
            console.log(formdata);
            $.ajax({ 
                url:wpAjax.ajaxUrl,
                data:formdata,
                type:'POST',
                dataType: 'json',
                success:function(response){
                    console.log(response);
                    if(response.status == 1) {
                        alert('user exist');
                        console.log("UserCorrecte");
                    }
                    if(response.status !== 1) {
                        alert("User does not exist");
                        console.log(response.errorstype);
                    }
                },
                error: function (request, status, error) {
                    console.log(request.responseText);
                }
            });
        });
    });
});
7
  • 1
    A 500 error is typically a coding error. Is there anything in your PHP error log? If you look at the actual response to the request in the browser's dev tools what do you see? Commented Aug 12, 2022 at 14:17
  • I have already follow this advice but I don't know if I well understand :
    – imagIne
    Commented Aug 12, 2022 at 14:32
  • I have already follow this advice but I don't know if I well understand : "POST /wp-admin/admin-ajax.php HTTP/1.1" 200 47 website.com "https://website.com/wp-admin/. The log says me that ajax is well called from main domain but the console says it called from subdomain status of 500 () : https://sub1.domain.com/wp-admin/admin-ajax.php. I have already verify if called several time Ajax , but this is not the case
    – imagIne
    Commented Aug 12, 2022 at 14:40
  • that wouldn't cause a HTTP 500 on its own, you need to look at your PHP error log to find out what the actual error is. HTTP 500 is a super generic something went wrong message
    – Tom J Nowell
    Commented Aug 12, 2022 at 15:45
  • I currently searching my error, but could you please tell me if each error calling admin-ajax.php define an error before to call this one or not necessary ? Now I have error 400 after an oversight and i can't find any error with Jquery or Ajax.
    – imagIne
    Commented Aug 13, 2022 at 9:20

0

Your Answer

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

Browse other questions tagged or ask your own question.