【问题标题】:Redirect to the an id of a page with the header function in PHP isn't working使用 PHP 中的 header 函数重定向到页面的 id 不起作用
【发布时间】:2014-05-10 03:34:11
【问题描述】:

我正在创建一个网站,该网站需要使用 PHP 从一个页面重定向到同一页面的 id。当 header 被调用时,它只是重定向到没有指定 id 的页面,即使在 header 函数中调用它来重定向到具有 id 的页面部分。

它应该重定向到:

header('Location: '.$findHttp.'://'.$_SERVER["HTTP_HOST"].'/~a7068104/2013-2014/Lab_13/Reports/Reports.php#redirect');

但它实际上指向:

header('Location: '.$findHttp.'://'.$_SERVER["HTTP_HOST"].'/~a7068104/2013-2014/Lab_13/Reports/Reports.php');

我的代码是否存在导致此问题的问题。 (顺便说一下,这是另一个问题的延续,但我的代码没有发布在另一个问题中,即使我想删除,我也无法删除另一个问题。)

这是我的代码:

<?php
if (isset($_POST['reportsubmit'])) {
    $radio = $_POST['report'];
    if ($radio == 'customer') {
        $redirect = 'Click <a href="#customer">here</a> to continue on with the form';
    } else if ($radio == 'item') {
        $redirect = 'Click <a href="#item">here</a> to continue on with the form';
    } else if ($radio == 'department') {
        $redirect = 'Click <a href="#department">here</a> to continue on with the form';
    } else if ($radio == 'person') {
        $redirect = 'Click <a href="#person">here</a> to continue on with the form';
    }
    $findHttp = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https' : 'http';
    header('Location: '.$findHttp.'://'.$_SERVER["HTTP_HOST"].'/~a7068104/2013-2014/Lab_13/Reports/Reports.php#redirect');
} else if (isset($_POST['customersubmit'])) {
    //process form
    //redirect
    exit;
} else if (isset($_POST['itemsubmit'])) {
    //process form
    //redirect
    exit;
} else if (isset($_POST['departmentsubmit'])) {
    //process form
    //redirect
    exit;
} else if (isset($_POST['personsubmit'])) {
    //process form
    //redirect
    exit;
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Gordmart MIS Reports</title>
    <!--<link rel="stylesheet" href="../css/Main.css">-->
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.css">
    <script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.js"></script>
</head>
<body>
    <div data-role="page" class="frame" id="report">
        <div data-role="header">
            <?php include("Header.php");?>
        </div>
        <div data-role="main" id="main">
            <h3>Would you like to view a report grouped by customers, items, sales departments, or sales people?</h3>
            <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF;"], ENT_QUOTES, "utf-8"); ?>" method="post">
                <input type="radio" name="report" value="customer"><p>Customers</p>
                <input type="radio" name="report" value="item"><p>Items Sold</p>
                <input type="radio" name="report" value="department"><p>Sales Departments</p>
                <input type="radio" name="report" value="person"><p>Sales People</p>
                <input type="submit" name="reportsubmit" value="Submit">
            </form>
        </div>
        <div data-role="footer">
            <h1>Footer Text</h1>
        </div>
    </div>
    <div data-role="page" class="frame" id="customer">
        <div data-role="header">
            <?php include("Header.php");?>
        </div>
        <div data-role="main" id="main">
            <h3>Would you like to view a cumulative report of all customers, or a single report of just one?</h3>
            <form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF'], ENT_QUOTES, 'utf-8'); ?>" method="post">
                <input type="radio" name="customer" value="all"><p>All</p>
                <input type="radio" name="customer" value="one"><p>One</p><br>
                <input type="submit" name="customersubmit" value="Submit">
            </form>
        </div>
        <div data-role="footer">
            <h1>Footer Text</h1>
        </div>
    </div>
    <div data-role="page" class="frame" id="item">
        <div data-role="header">
            <?php include("Header.php");?>
        </div>
        <div data-role="main" id="main">
            <h3>Would you like to view a cumulative report of all sales items, or a single report of just one?</h3>
            <form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF'], ENT_QUOTES, 'utf-8'); ?>" method="post">
                <input type="radio" name="item" value="all"><p>All</p>
                <input type="radio" name="item" value="one"><p>One</p><br>
                <input type="submit" name="itemsubmit" value="Submit">
            </form>
        </div>
        <div data-role="footer">
            <h1>Footer Text</h1>
        </div>
    </div>
    <div data-role="page" class="frame" id="department">
        <div data-role="header">
            <?php include("Header.php");?>
        </div>
        <div data-role="main" id="main">
            <h3>Would you like to view a cumulative report of all sales departments, or a single report of just one?</h3>
            <form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF'], ENT_QUOTES, 'utf-8'); ?>" method="post">
                <input type="radio" name="department" value="all"><p>All</p>
                <input type="radio" name="department" value="one"><p>One</p><br>
                <input type="submit" name="departmentsubmit" value="Submit">
            </form>
        </div>
        <div data-role="footer">
            <h1>Footer Text</h1>
        </div>
    </div>
    <div data-role="page" class="frame" id="person">
        <div data-role="header">
            <?php include("Header.php");?>
        </div>
        <div data-role="main" id="main">
            <h3>Would you like to view a cumulative report of all sales people, or a single report of just one?</h3>
            <form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF'], ENT_QUOTES, 'utf-8'); ?>" method="post">
                <input type="radio" name="person" value="all"><p>All</p>
                <input type="radio" name="person" value="one"><p>One</p><br>
                <input type="submit" name="personsubmit" value="Submit">
            </form>
        </div>
        <div data-role="footer">
            <h1>Footer Text</h1>
        </div>
    </div>
    <div data-role="page" class="frame" id="redirect">
        <div data-role="header">
            <?php include("Header.php");?>
        </div>
        <div data-role="main" id="main">
            <?php echo $redirect;?>
        </div>
        <div data-role="footer">
            <h1>Footer Text</h1>
        </div>
    </div>
</body>
</html>

【问题讨论】:

    标签: php redirect


    【解决方案1】:

    这是因为哈希是浏览器实用程序。据我了解,您根本无法从服务器端 (PHP) 重定向到带有哈希的 URL。

    执行此操作的唯一方法是通过 javascript。 Here's an answer to a previous page 展示了如何在 JavaScript 中执行此操作。在您的情况下,您必须捕获表单提交,然后使用适当的哈希调用 window.location.replace

    【讨论】:

      【解决方案2】:

      哈希部分永远不会发送到服务器。它仅存在于网络浏览器中,因此您必须编写 javascript 代码才能获取它。然后你当然可以在 javascript 中将它发送到服务器。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-16
        • 2021-12-26
        • 1970-01-01
        • 2011-11-25
        • 1970-01-01
        • 1970-01-01
        • 2012-11-17
        • 2012-03-16
        相关资源
        最近更新 更多