I want to run a script on a php file but it pass not running it. It run the header before the function runs son i dont know how to run first the script
iniciales($us,$ap);
header('Location: Lista_libros.php');//This runs before the funtion
function iniciales($ini1,$ini2){
echo ("entro ");
$inicial1 = substr($ini1, 0, 1);
$inicial2 = substr($ini2, 0, 1);
echo ($inicial1);
echo ($inicial2);
echo '<script type="text/javascript">
alert("hjfoshf");
var x = "'.$inicial1.'";
var y = "'.$inicial2.'";
let usuario = {nom: x , ape: y};
localStorage.setItem("usuario", usuario );
</script>';
}
CodePudding user response:
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
CodePudding user response:
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 .