php - Javascript: header is not defined - Stack Overflow

I already tried the 2 re-directions in JavaScript, the window.location.href() and header() but always s

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744314944a4568133.html

相关推荐

  • php - Javascript: header is not defined - Stack Overflow

    I already tried the 2 re-directions in JavaScript, the window.location.href() and header() but always s

    7天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信