-2

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>';
}
behzad m salehi
  • 1,038
  • 9
  • 23
  • 1
    You are a bit short on useful context in this question – RiggsFolly Oct 26 '22 at 10:26
  • 1
    Welcome, to improve your experience on SO please [take the tour](http://stackoverflow.com/tour) **like you were asked to when you registered** and read [how to ask](https://stackoverflow.com/help/how-to-ask), an [On Topic question](https://stackoverflow.com/help/on-topic), then look at the [Question Check list](https://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist), the [perfect question](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) and how to create a [Minimal, Complete and Verifiable Example](http://stackoverflow.com/help/mcve) – RiggsFolly Oct 26 '22 at 10:26
  • 1
    This could well be a [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – RiggsFolly Oct 26 '22 at 10:27
  • `header()` won't work if there's content before. Try redirecting from your JS – brombeer Oct 26 '22 at 10:28

2 Answers2

0

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

behzad m salehi
  • 1,038
  • 9
  • 23
0

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 .

  • Unless you use `node.js`... then Javascript is a server-side script language as well ;) – Honk der Hase Oct 26 '22 at 18:28
  • Node.js is an open-source, cross-platform, back-end JavaScript runtime environment that runs on a JavaScript Engine and executes JavaScript code outside a web browser – Isfhan Ahmed Oct 27 '22 at 06:54