doulu8537 2019-03-27 13:45
浏览 44
已采纳

为什么这个PHP代码在从用户输入创建动态链接时第二次运行正常?

I am trying to create a query screen for reports. I have created a php code by getting support from here and other sites. But the problem is; when a user input report serial number and submits it, the page only reload. After reload; when the user enters serial again to the field and hits submit, this time the code works but only for the 1st serial entered, no matter the second serial is.

I have tried to change the parts of my code but could not find a solution.

I am trying to create a system like, user will enter a serial to the field and when hits to submit button; a new window pop out and directs user to a link which has been created based on user input.

For example, user entered "234" as the serial number and hit submit button. The new window will go to the; "example.com/reports/report234.pdf"

Here is the code I have problem with;

<?php
if(isset($_POST['submit']))
{
    $seri = $_POST['seri'];
    $url = "https://www.example.com/wp-content/uploads/Rapor/".$seri.".pdf";    
}
?>
<form method="post" action="<?php echo $url; ?>">
    <input type="text" name="seri"><br>
    <input type="submit" name="submit" value="Sorgula"><br>
</form>
  • 写回答

2条回答 默认 最新

  • dongtaochan0777 2019-03-27 14:00
    关注

    That's because you're setting the redirect $url as the form action. That results in the following:

    1. Form action is empty, thus the form will be sent to the page itself
    2. The serial number and the $url is created and set as the form action
    3. Now when the user submits the form again it will be directed to the $url set, regardless of what he has filled in the seri field this time

    Here is an example of a more correct approach to your problem:

    <?php
    if(isset($_POST['submit']))
    {
        $seri = $_POST['seri'];
        Header("Location: https://www.ozguncicek.com.tr/wp-content/uploads/Rapor/$seri.pdf");
    }
    ?>
    <form method="post">
        <input type="text" name="seri"><br>
        <input type="submit" name="submit" value="Sorgula"><br>
    </form>
    

    Note there's no need to set an action to the form since you're going to redirect the user when he submits the form.

    Another important point is checking if seri isn't empty before redirecting the user. That could be accomplished as simple as:

    <?php
        if(isset($_POST['submit']) && $_POST['seri'])
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用