简体   繁体   中英

Adding toast in login when password or username is wrong

<button type='submit' action='#' class='btn btn-primary block full-width m-b' id='showsimple1'>Login</button>

$name = $_POST['name'];
$password = $_POST['password'];


if(!($usr = $db->getRow("SELECT `userid` FROM `".MLS_PREFIX."users` WHERE `username` = ?s AND `password` = ?s", $name, sha1($password))))
    $(function () {
    $('#showsimple1').click(function (){
        // Display a error toast, with a title
        toastr.options = {
          "closeButton": true,
          "debug": false,
          "progressBar": true,
          "preventDuplicates": true,
          "positionClass": "toast-top-right",
          "onclick": null,
          "showDuration": "400",
          "hideDuration": "1000",
          "timeOut": "7000",
          "extendedTimeOut": "1000",
          "showEasing": "swing",
          "hideEasing": "linear",
          "showMethod": "fadeIn",
          "hideMethod": "fadeOut"
        }
        toastr.error('Username or password are wrong!')
    });            

})

else {
    if($_POST['r'] == 1){
        $path_info = parse_url($set->url);
        setcookie("user", $name, time() + 3600 * 24 * 30, $path_info['path']); // set
        setcookie("pass", sha1($password), time() + 3600 * 24 * 30, $path_info['path']); // set
    }
    $_SESSION['user'] = $usr->userid;
    header("Location: $set->url/home.php");
    exit;
}

The first code is my submit button

I cannot manage to make this work, I've been trying to pop up toast when the password or username input is incorrect. There's always parse error in my code.

As you want to show error message when the page is submit and then it load back then you don't have to put the code for showing the code inside the click listener like this. It will be like this

if(!($usr = $db->getRow("SELECT `userid` FROM `".MLS_PREFIX."users` WHERE `username` = ?s AND `password` = ?s", $name, sha1($password))))
        $(function () {
            // Display a error toast, with a title
            toastr.options = {
              "closeButton": true,
              "debug": false,
              "progressBar": true,
              "preventDuplicates": true,
              "positionClass": "toast-top-right",
              "onclick": null,
              "showDuration": "400",
              "hideDuration": "1000",
              "timeOut": "7000",
              "extendedTimeOut": "1000",
              "showEasing": "swing",
              "hideEasing": "linear",
              "showMethod": "fadeIn",
              "hideMethod": "fadeOut"
            }
            toastr.error('Username or password are wrong!')
        });            

    else {
        if($_POST['r'] == 1){
            $path_info = parse_url($set->url);
            setcookie("user", $name, time() + 3600 * 24 * 30, $path_info['path']); // set
            setcookie("pass", sha1($password), time() + 3600 * 24 * 30, $path_info['path']); // set
        }
        $_SESSION['user'] = $usr->userid;
        header("Location: $set->url/home.php");
        exit;
    }

I am just pointing where you went wrong. It's not a good practice to mix php with the html, css or js. You can go with MVC(Model View Controller) Architecture

Write javscript code inside <script> </script> tag

        <link href="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/2.0.1/css/toastr.css" rel="stylesheet"/>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/2.0.1/js/toastr.js"></script>


         <form method="post" >
        <input name="name" type="text">
        <input name="password" type="text">
        <button type='submit' action='#' class='btn btn-primary block full-width m-b' id='showsimple1'>Login</button>
        </form>


        <?php

         if(!($usr = $db->getRow("SELECT `userid` FROM `".MLS_PREFIX."users` WHERE `username` = ?s AND `password` = ?s", $name, sha1($password))))
{ ?>
            <script>
                $(function () {
                    // Display a error toast, with a title
                    toastr.options = {
                      "closeButton": true,
                      "debug": false,
                      "progressBar": true,
                      "preventDuplicates": true,
                      "positionClass": "toast-top-right",
                      "onclick": null,
                      "showDuration": "400",
                      "hideDuration": "1000",
                      "timeOut": "7000",
                      "extendedTimeOut": "1000",
                      "showEasing": "swing",
                      "hideEasing": "linear",
                      "showMethod": "fadeIn",
                      "hideMethod": "fadeOut"
                    }
                    toastr.error('Username or password are wrong!')
                });            
            </script> 
        <?php
            }else {
                if($_POST['r'] == 1){
                    $path_info = parse_url($set->url);
                    setcookie("user", $name, time() + 3600 * 24 * 30, $path_info['path']); // set
                    setcookie("pass", sha1($password), time() + 3600 * 24 * 30, $path_info['path']); // set
                }
                $_SESSION['user'] = $usr->userid;
                header("Location: $set->url/home.php");
                exit;
            } ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM