简体   繁体   English

在同一页面上执行表单操作后,重定向到PHP中的另一个页面

[英]Redirect to another page in PHP after performing form action on same page

So I have my page as downloads.php and that is where my form is located as well as it's form action. 所以我将我的页面作为downloads.php ,这就是我的表单所在的位置以及它的表单操作。 The downloads.php 's contents are as shown below downloads.php的内容如下所示

<form method="POST" action="">
<td><button type="submit" name="download" value="<?php echo $g['filename'];?> ">Download</button></td>
</form>
<?php
    if( isset($_POST['download']) ){
        $file = $_POST["download"];


        $sql = "UPDATE files SET downloads=1+downloads WHERE filename ='$file'";
        $conn->query($sql);


    //I want to redirect users to portal after updating my database
        header("Location: portal.php");

    }
?>

I've used header though I'm absolutely puzzled why it doesn't work. 我已经使用了header虽然我很困惑为什么它不起作用。

Note in the PHP docs for header() that header() must be executed before any HTML is output. 请注意,在header()PHP文档中,必须在输出任何HTML之前执行header()。 Try putting all your PHP code at the top of the document and it should work 尝试将所有PHP代码放在文档的顶部,它应该可以工作

I found a quick way to do it with javascript and that is 我用javascript找到了一个快速的方法,就是这样


    <?php
        if( isset($_POST['download']) ){
            $file = $_POST["download"];
            $sql = "UPDATE files SET download=1+download WHERE filename ='$file'";
            $conn->query($sql);
    ?>
    <script>
    var str1 = 'https://www.example.com/portal.php';
    window.location.href = str1;
    </script>

    <?php
    }
    ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM