duanlian1960 2018-05-21 08:23
浏览 71

提交重定向到另一个页面后不使用PHP

I have couple of identical HTML pages which take user input and with PHP I save the input on text files. As it's always the same I would like to apply the same PHP on every html page. So my question is if there is a way to redirect to the next html page but not through PHP, so that the PHP can be reusable for all the pages?

Because if I add onclick="window.location.href='/main2.html' then the PHP is not fired up or if I change action="main2.html" then again the PHP is not included. Or if I add header("Location: main2.html"); then it cannot really be applied to all the html pages, as it goes main.html -> main2.html -> main3.html etc.

HTML 1:

<form method="post" action="process.php">
        <input type="text" name="address" required>

        <input type="submit" value="Submit" id="intro">
</form>

HTML 2:

<form method="post" action="process.php">
        <input type="text" name="address" required>

        <input type="submit" value="Submit" >
</form>

PHP:

<?php
    $myfile = fopen("text.txt", "a+");

    $address = $_POST['address'].";";
    fwrite($myfile, $address);

    //header("Location: main2.html");
    fclose($myfile);
?>

Thank you in advance!

  • 写回答

1条回答 默认 最新

  • dongxiaoyan4388 2018-05-21 09:17
    关注

    Although you don't want to use PHP, it can make this pretty simple for you. You can use the session variable in PHP to track the user, the entries he has made and even allow them to go back and forward. You can read more on it here.

    You can redirect the user based on the session counter or the source from where the call is made and direct them accordingly to the next page. This makes your code scalable as well.

    So you code will be something like this:

       session_start();
    
       if( isset( $_SESSION['counter'] ) ) {
          $_SESSION['counter'] += 1;
       }else {
          $_SESSION['counter'] = 1;
       }
    
       $newURL = "HTMLPage".$_SESSION['counter']."html";
       header('Location: '.$newURL);
    

    The above code will redirect the user to the next page. PS: You will have to handle the case for your last html page.

    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么