0

I want to be able to show an output to the user when he tries to log in or sign up and he isn't able to. Ideally I want to be able to call a JavaScript function from a file or at least call prompt() or something like that. The code is the following:

<?php
include("../php/functions.php");
include("../php/connect.php");

do{
    if(isset($_POST['submit-btn'])) {
        $loggedIn = authenticate($conn, $_POST['email'], $_POST['password']);
        if($loggedIn === null) {
            /**TODO: Do something when the database couldn't be reached */
            break;
        }

        if(!$loggedIn) {
            /***TODO: Do something when the password is incorrect*/
            break;
        }
        if($loggedIn)
            header("Location: ../html/homepage.html");
    }
}while(false)

?>

I get the data from $_POST['*value*'] after the user has submited it and I don't know how to show a, somewhat, presentable error message to he user.

  • 1
    You can [echo](https://www.php.net/manual/en/function.echo.php) any text message or HTML elements and also ` – brombeer May 20 '22 at 13:51
  • I know but I don't I will do this as a last resort – Simos Neopoulos May 20 '22 at 13:54
  • Do what as a last resort? "_I want to be able to show an output to the user_" sounds like you want to ... output something to the user, that's (usually) done by `echo`ing something – brombeer May 20 '22 at 13:55
  • I have seen outputs like that in other posts but they say that they don't call the functions that you write in the echo. – Simos Neopoulos May 20 '22 at 13:58
  • I' sorry but I don't quite understand that last comment. You can also step out of PHP using `?>` and add regular HTML code if you don't want to use `echo` or functions. Remember to step in using ` – brombeer May 20 '22 at 14:03
  • Does this answer your question? [PHP Print and Echo HTML](https://stackoverflow.com/questions/4033051/php-print-and-echo-html) – Homer512 May 23 '22 at 13:05

1 Answers1

1

Showing an output to the user on an unsuccessful login should be as simple changing your code from this:

if(!$loggedIn) {
    /***TODO: Do something when the password is incorrect*/
    break;
}

to this:

if(!$loggedIn) {
    echo "<p>Login unsuccessful!</p>";
    break;
}

If you want to call a JavaScript function you also can do it with the same approach:

if(!$loggedIn) {
    echo "<script>function warning() { alert('Wrong password!'); } warning();</script>";
    break;
}