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 function getParameterByName
was extracted from here:
https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript
Your code does not execute because when redirecting the page with the Location header in PHP the code below in javascript is not executed by the browser
Your line
header("Location")
redirects the user to the pageindex.html
and the entire code stream will be ignored by the browser. If the intention is to display the alert and not redirect, remove this line.– Woss
Even, it’s good to totally eliminate Alert. Put a time interval in Location as a temporary solution. <meta http-equiv="refresh" content="5; url=http://example.com/">, so your Alert remains intrusive (like all Alert) but works
– Bacco