0

I have a web age containing both PHP(7.3) which is first in the file and HTML(5) shown below.

In the PHP, I do the following:

echo '<script>gotoLogIn();</script>';

In the HTML, I have the following:

<script>
function gotoLogIn() {
    alert("Now going elsewhere.");
    window.location.replace("http://localhost/xxxx/yyyy/zzzz/index.php");
}
</script>

When the PHP "echo" is executed, I get "Uncaught ReferenceError: gotoLogIn is not defined which shows up on the Firefox Web Console". I get the same error on Chrome.

I've tried a number of things including the answer to a similar question on SO with no joy.

I am at a loss. This is the technique I have found in several sites.

Are they old references and is there a newer way to do this or a security action that needs to be taken.

All help appreciated.

Regards, Jim

user1047857
  • 113
  • 1
  • 10
  • 1
    You can use php's `header()`, don't see a reason for doing it from javascript – Alon Eitan Sep 25 '20 at 18:09
  • Ignore the PHP, and just look at the source code for the browser. Make sure that the function is being defined somewhere on the page – aynber Sep 25 '20 at 18:09
  • I agree with @AlonEitan - this is a JavaScript problem but there is no need to use JS for simple redirection. – VLAZ Sep 25 '20 at 18:21
  • 1
    In what order do the ` – Jonathan Lonowski Sep 25 '20 at 18:48

1 Answers1

0

Well as the others already mentioned you can do it with PHP header (https://www.php.net/manual/de/function.header.php)

header("LOCATION: index.php");

If you still want to use JavaScript you have to ensure that your function called by php is executed after the Script has been defined within your html.

You can do this the following way have a look at this thread Pure JavaScript equivalent of jQuery's $.ready() - how to call a function when the page/DOM is ready for it

Marc
  • 14
  • I appreciate the input but what I showed is just a sample of what I am doing and which is much more than the example. The redirection is that last step. The sample is what I am testing with and it does not work. – user1047857 Sep 25 '20 at 20:04