PHP使用ajax如果还没有工作和md5()?

when i execute my javascript this code why just else statement is working but not if,and when i use md5($_POST['password']) can't login?? but when not using md5 everything is ok

help me please :)

this is my database

image

this is my javascript

<script type="text/javascript">
    $(document).ready(function() {
        $("form#form_login").submit(function(event){ 
            event.preventDefault();
            var formData = new FormData($(this)[0]);
            $.ajax({
                type:'POST',
                url :'../assets/js/utama/login.data.php',
                data:formData,
                async:false,
                cache:false,
                contentType:false,
                processData:false,
                success:function(data){ 
                    if(data == "success")
                    {
                        window.location = '../index.php?hal=home';
                    }else{
                        alert('error');
                    }
                }
            });
        });
        return false;
    });
    </script>

this is my php file

<?php
session_start();
if(isset($_POST['email'])){
    include "../../../konten/koneksi.php";
    $email = $_POST['email'];
    $pass  = md5($_POST['password'])
    $sql_login = "select * from user where email_user ='$email ' AND password_user='$pass'";
    $run_login = mysql_query($sql_login);
    $data = mysql_fetch_array($run_login);
    if(isset($data['email_user'])){
        $_SESSION['email_user'] = $data['email_user'];
        $_SESSION['status'] = $data['status'];
    }else{
        echo "alert('errorr')";
    }
}
?>

Yes It will always go in else part because you haven't eco success from php file then how can it will be go in success!!!?? change your php script to

<?php
session_start();
if(isset($_POST['email'])){
    include "../../../konten/koneksi.php";
    $email = $_POST['email'];
    $pass  = md5($_POST['password']);
    $sql_login = "select * from user where email_user ='$email ' AND password_user='$pass'";
    $run_login = mysql_query($sql_login);
    $data = mysql_fetch_array($run_login);
    if(isset($data['email_user'])){
        $_SESSION['email_user'] = $data['email_user'];
        $_SESSION['status'] = $data['status'];
        echo "success";
    }else{
        echo "error";
    }
}
?>

Also mark that don't use mysql* its deprecated and completely remove in PHP7. Use mysqli* or PDO instead

In your code, in your select query there is space after $email and also you need to echo success when your if condition is executed. Improvise your php code as below:

<?php
    session_start();
    if(isset($_POST['email'])){
        include "../../../konten/koneksi.php";
        $email = $_POST['email'];
        $pass  = md5($_POST['password']);
        $sql_login = "select * from user where email_user ='".$email."' AND password_user='".$pass."'";
        $run_login = mysql_query($sql_login);
        $data = mysql_fetch_array($run_login);
        if(isset($data['email_user'])){
            $_SESSION['email_user'] = $data['email_user'];
            $_SESSION['status'] = $data['status'];
           echo "success";
        }else{
            echo "alert('errorr')";
        }
    }
    ?>

Hope this will help!

  • You need to echo "success" in your PHP script response.
  • semicolon(;) missing md5($_POST['password']);

    $email = $_POST['email'];
    $pass  = md5($_POST['password']);
    $sql_login = "select * from user where email_user ='$email' AND password_user='$pass'";
    $run_login = mysql_query($sql_login);
    $data = mysql_fetch_array($run_login);
    if(isset($data['email_user'])){
        $_SESSION['email_user'] = $data['email_user'];
        $_SESSION['status'] = $data['status'];
        echo "success";
    }else{
        echo "error";
    }
    

Please make sure all "password_user" column contain the md5 string in Database. So you have to store md5 for a password at the time of user registration. You have one non-md5 entry in the database for user "reza". So for this user md5($_POST['password']) will not match in database.

And write echo 'success'; in IF statement in PHP script.