I want to run a script on a PHP file, but it is not running. It runs the header before the function runs so don’t know how to run first the script
iniciales();
header('Location: Lista_libros.php');//This runs before the funtion
function iniciales(){
echo '<script type="text/javascript">
alert("test");
</script>';
}
2
Answers
it will actually run, but you can’t see it because of redirection,
well, if the server sends a redirection header, the browser redirects and therefore "changes the URL", so your alert and other kinds of stuff will be wiped out from the screen
PHP is a server side script language.
JavaScript is client side script language.
So in your case if you want to run JavaScript code try redirecting from your JavaScript and remove header function form PHP code .
If you do this so your JavaScript code can execute .