【问题标题】:Button onclick open window when data is set设置数据时按钮 onclick 打开窗口
【发布时间】:2021-03-03 11:11:20
【问题描述】:

我最初的想法是,当提交表单时,我获得了通过 javascript 使用window.open() 下载正确内容所需的 ID,但它被阻止了,因为它不是直接来自按钮单击。那么如何在设置按钮后让它成为 onlclick 事件的一部分呢?

PHP:

if (isset($_POST['redownload'])) {

    $row3 = Users::redownloadContent($_POST['redownload_id']);

    ?>
    <script>
        window.open('<?php echo $row3["content"] ?>');
    </script>
    <?php
}

HTML:

<input type="hidden" name="redownload_id" value="<?php echo $row['id'] ?>">
<button class="btn btn-secondary" type="submit" name="redownload">Download latest version</button>

【问题讨论】:

    标签: javascript php html forms button


    【解决方案1】:

    只需使用重定向:

    if (isset($_POST['redownload'])) {
    
        $row3 = Users::redownloadContent($_POST['redownload_id']);
    
        header("Location: ".$row3["content"]);
    }
    

    如果你想使用 JS,你可以这样做:

    if (isset($_POST['redownload'])) {
    
        $row3 = Users::redownloadContent($_POST['redownload_id']);
    
        ?>
        <script>
            window.location.href = "<?php echo $row3["content"] ?>";
        </script>
        <?php
    }
    

    【讨论】:

      猜你喜欢
      • 2013-09-22
      • 2020-12-13
      • 1970-01-01
      • 1970-01-01
      • 2011-06-06
      • 2017-06-14
      • 2012-08-31
      • 2018-12-18
      • 1970-01-01
      相关资源
      最近更新 更多