How to make an alert in php [closed]

0

Give an alert or other type of message after the user misses the password on the login page for example? I tried to do it this way:

    unset ($_SESSION['login']);
unset ($_SESSION['senha']);
header('location:index.html');
echo '<script language="javascript">';
echo 'alert("Erro ao fazer Login")';
echo '</script>';

But it did not work, it goes back to the screen, but it does not alert

    
asked by anonymous 24.10.2017 / 23:01

1 answer

2

Send parameters with query string to index.html and in index add the alert script.

<?php
unset ($_SESSION['login']);
unset ($_SESSION['senha']);
header('location:index.html?info=error&msg=1');

And in index.html add the script:

<script>
    function getParameterByName(name, url) {
        if (!url) url = window.location.href;
        name = name.replace(/[\[\]]/g, "\$&");
        var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
            results = regex.exec(url);
        if (!results) return null;
        if (!results[2]) return '';
        return decodeURIComponent(results[2].replace(/\+/g, " "));
    }

    if(getParameterByName('info') === 'error' && getParameterByName('msg') === '1') {
        alert('Erro ao fazer Login');
    }
</script>

The getParameterByName function has been extracted from here: link

Your code does not run because by redirecting the page with the header location in PHP the code below in javascript is not executed by the browser

    
24.10.2017 / 23:13