admin管理员组

文章数量:1410737

I already tried the 2 re-directions in JavaScript, the window.location.href() and header() but always show me an error:

ReferenceError: header is not defined

My code from PHP:

echo "<script> header('location: index.php');</script>";

I already tried the 2 re-directions in JavaScript, the window.location.href() and header() but always show me an error:

ReferenceError: header is not defined

My code from PHP:

echo "<script> header('location: index.php');</script>";
Share Improve this question edited Oct 3, 2016 at 5:21 Rax Weber 3,78020 silver badges30 bronze badges asked Oct 3, 2016 at 4:24 Agel SalazarAgel Salazar 31 silver badge2 bronze badges 1
  • 1 header() is a php function, that is why it is undefined in JavaScript. Just header('location: index.php'); exit; is required. Don't echo it though. – Rasclatt Commented Oct 3, 2016 at 4:27
Add a ment  | 

4 Answers 4

Reset to default 5

There's no header() function in JavaScript. header() is a PHP function. Here is one way to redirect using JS:

echo "<script> window.location.href = 'index.php'; </script>";

Or in PHP:

header('Location: index.php');
exit;

you dont need to use jquery try this

echo  header('Location: index.php');
exit;

You can easily achieve with Below Code:

For JavaScript in PHP:

echo '<script>window.location.href="YOURURL.COM"</script>';

For Simple PHP:

echo header('Location: '."YOURURL.COM");

header() is a php function. and header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.

<?php
header("Location: index.php"); /* Redirect browser */

/* Make sure that code below does not get executed when we redirect. */
exit;
?>

ref: http://php/manual/en/function.header.php

in js try, window.location.href = "index.php" //redirectUrl

本文标签: phpJavascript header is not definedStack Overflow