AJAX Success Returns and Errors

I created a login script that uses AJAX to submit form data.

Part of PHP works fine without AJAX. But the system does not work with the implementation of AJAX.

It always displays the message below, even if the PHP file returns true [valid username and password] ... It seems that the if condition does not work in JQuery.

Invalid username / password

HTML Div Result

<div id="user-result" align="center"></div>

JQuery

<script type="text/javascript">
    $(document).ready(function () {
        var form = $('#loginform');
        form.submit(function (ev) {
            ev.preventDefault();
            $.ajax({
                type: form.attr('method'),
                url: form.attr('action'),
                cache: false,
                data: form.serialize(),
                success: function (data) {
                    if (data == "true") {
                        $("#user-result").html("<font color ='#006600'> Logged in | Redirecting..</font>").show("fast");
                        setTimeout(
                            function () {
                                window.location.replace("index.php");
                            }, 1500);
                    } else {
                        $("#user-result").html("<font color ='red'> Incorrect Username/Password</font>").show("fast");
                    }

                }

            });
        });
    }); 
</script>

fn_login.php

<?php
{
    session_start();
    include_once 'db_connect.php';

    if (isset($_POST))
     {
        $email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_STRING);
        $logpwd = filter_input(INPUT_POST, 'password', FILTER_SANITIZE_STRING);

        $stmt = $conn->prepare("SELECT password FROM manager WHERE email = ? LIMIT 1");
        $stmt->bind_param("s", $email);


        $stmt->execute();
        $stmt->store_result();

        // get variables from result.
        $stmt->bind_result($password);
        $stmt->fetch();


        // Check if a user has provided the correct password by comparing what they typed with our hash
        if (password_verify($logpwd, $password))
        {
            $sql = "SELECT * from manager WHERE email LIKE '{$email}' LIMIT 1";
            $result = $conn->query($sql);
            $row=mysqli_fetch_array($result);
            $id = $row['id'];
            $conn->query("UPDATE manager SET lastlogin = NOW() WHERE id = $id");

            $_SESSION['manager_check'] = 1;
            $_SESSION['email'] = $row['email'];
            $_SESSION['fullname'] = $row['fullname'];
            $_SESSION['designation'] = $row['designation'];
            $_SESSION['id'] = $row['id'];
            echo "true";
        }
         else {
            die();
        }
     }
}

?>      

Someone may point out a bug in the code / practice.

EDIT

Just by trying to disable AJAX, the PHP file works correctly, an echo truewhen the username / password is correct

+4
source share
2 answers

You have spaces after ?>

, AJAX true.

:

?> PHP.

PHP.

AJAX .

?> PHP PHP CMS.

AJAX:

1) Firefox ( Firebug Add) Chrome.

2) Console Firebug, , AJAX .

3) , .

4) , , AJAX.

+2

json_encode(array('success'=>true)) php- if jquery if(data.success){}.

<?php
{
    session_start();
    include_once 'db_connect.php';

    if (isset($_POST))
     {
        $email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_STRING);
        $logpwd = filter_input(INPUT_POST, 'password', FILTER_SANITIZE_STRING);

        $stmt = $conn->prepare("SELECT password FROM manager WHERE email = ? LIMIT 1");
        $stmt->bind_param("s", $email);


        $stmt->execute();
        $stmt->store_result();

        // get variables from result.
        $stmt->bind_result($password);
        $stmt->fetch();


        // Check if a user has provided the correct password by comparing what they typed with our hash
        if (password_verify($logpwd, $password))
        {
            $sql = "SELECT * from manager WHERE email LIKE '{$email}' LIMIT 1";
            $result = $conn->query($sql);
            $row=mysqli_fetch_array($result);
            $id = $row['id'];
            $conn->query("UPDATE manager SET lastlogin = NOW() WHERE id = $id");

            $_SESSION['manager_check'] = 1;
            $_SESSION['email'] = $row['email'];
            $_SESSION['fullname'] = $row['fullname'];
            $_SESSION['designation'] = $row['designation'];
            $_SESSION['id'] = $row['id'];
            echo json_encode(array('success'=>true));
        }
         else {
            die();
        }
     }
}

And jQuery is getting

<script type="text/javascript">
    $(document).ready(function () {
        var form = $('#loginform');
        form.submit(function (ev) {
            ev.preventDefault();
            $.ajax({
                type: form.attr('method'),
                url: form.attr('action'),
                cache: false,
                data: form.serialize(),
                success: function (data) {
                    if (data.success) {
                        $("#user-result").html("<font color ='#006600'> Logged in | Redirecting..</font>").show("fast");
                        setTimeout(
                            function () {
                                window.location.replace("index.php");
                            }, 1500);
                    } else {
                        $("#user-result").html("<font color ='red'> Incorrect Username/Password</font>").show("fast");
                    }

                }

            });
        });
    }); 
</script>
0
source

Source: https://habr.com/ru/post/1598347/


All Articles