Login PHP + COOKIE + AJAX

Asked

Viewed 30 times

-2

after more than 20 years without programming in PHP, I decided to make a "system" of scheduling clients for my girlfriend. But I am going through a problem that is leaving me with more gray hair. (rs)

$("#form_validation").click(function(){
                var username = $("#usuario").val().trim();
                var password = $("#password").val().trim();
                    
                if(username == "" || password == ""){
                    $("#modal_erro1").slideDown();
                    setTimeout(function() {
                   $('#modal_erro1').fadeOut('fast');
                    }, 3000);
                }else{          
                    $.ajax({
                        url:'includes/login.php',
                        type:'post',
                        data:'usuario='+username+'&senha='+password,
                        crossDomain: true,
                        success:function(response){

                            if(response == 1){
                                window.location.href="index.php";
                            }else{
                                $("#modal_erro1").slideDown();
                                    setTimeout(function() {
                                   $('#modal_erro1').fadeOut('fast');
                                    }, 3000);
                                    result.innerHTML="";
                            }
                        }
                    });
                    
                }
            });
<?php
 
    include('../../includes/conect.php');
    
    $usuario = $_POST["usuario"];
    $password = $_POST["senha"];
    
    $senha = hash('sha256', $password);
    
        $sql_user = "select id,user,pass from admin where user = '{$usuario}' and pass='{$senha}' limit 1";
        $result_user = $conn->query($sql_user);
                
        if ($result_user->num_rows > 0) {
            echo "1";
            $id_client = $result_user["id"];
            setcookie("id",$id_client,"",'/');
        }else{
            echo "0";
        }
    
        
 mysqli_close($conn);
 
 ?>

What happens is that the Cookie is not saved.

And another strange fact is that if I change the position echo within login.php, the answer returns null

Someone could give me a direction?

  • The cookie is not saved and certainly the login is not done. Already confirmed if the login performs? it enters inside this if?

  • Hello @BRABO thanks for the reply. Yes it enters inside the if and login is done. Testing includes/login.php page in GET

1 answer

-2

You’re missing a key in your js, after the $.ajax

Should stay like this:

$("#form_validation").click(function(){
            var username = $("#usuario").val().trim();
            var password = $("#password").val().trim();
                
            if(username == "" || password == ""){
                $("#modal_erro1").slideDown();
                setTimeout(function() {
               $('#modal_erro1').fadeOut('fast');
                }, 3000);
            }else{          
                $.ajax({
                    url:'includes/login.php',
                    type:'post',
                    data:'usuario='+username+'&senha='+password,
                    crossDomain: true,
                    success:function(response){

                        if(response == 1){
                            window.location.href="index.php";
                        }else{
                            $("#modal_erro1").slideDown();
                                setTimeout(function() {
                               $('#modal_erro1').fadeOut('fast');
                                }, 3000);
                                result.innerHTML="";
                             }
                        }
                    }
                });
                
            }
        });
  • Hello @Cristian Curtinaz Thanks for answering, but generated an error "Uncaught Syntaxerror: Missing ) after argument list".

Browser other questions tagged

You are not signed in. Login or sign up in order to post.