dongxian4531 2015-10-05 00:08
浏览 235
已采纳

使用$ _POST保持页面数据刷新

<!DOCTYPE html>
<html lang="en">
<head>
    <h1>Table Generator</h1>
</head>
<body>

<center><a href = "<?php $_SERVER['PHP_SELF']?>">Refresh</a></center>

<?php
    $rows = (isset($_POST['rows']) ? $_POST['rows'] : null);
    $cols = (isset($_POST['cols']) ? $_POST['cols'] : null);
    $highlight = (isset($_POST['highlight']) ? $_POST['highlight'] : null);

    if ($rows == "")
    {
        $rows = 10;
    }

    if ($cols == "")
    {
        $cols = 10;
    }

    if ($highlight == "")
    {
        $highlight = 5;
    }
?>
    <form method="post">
        ROWS <input type="text" name="rows" value = "<?php echo $rows;?>" /> 
        COLUMNS <input type="text" name="cols" value = "<?php echo $cols;?>" />
        HIGHLIGHT <input type = "text" name = "highlight" value = "<?php echo $highlight;?>" /><br>
        <input type="submit" value="Generate">
    </form>
<?php

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

    $randnumber = rand(0,100);

    $rows = $_POST['rows'];
    $cols = $_POST['cols'];
    $highlight = $_POST['highlight'];

    echo '<table border="1" align = "center">';

    if (is_numeric($rows) and is_numeric($cols) and is_numeric($highlight))
    {
        if ($randnumber % 2 == 0)
        {
            echo '<center>The first number is <div class = "red">even</div></center>';
        }

        else
        {
            echo '<center>The first number is <div class = "green">odd</div></center>';
        }

        for($row = 1; $row <= $rows; $row++)
        {
            echo '<tr style = "background-color:green">';

            for($col = 1; $col <= $cols; $col++)
            {
                if ($randnumber % $highlight == 0)
                {
                    echo '<td style = "background-color: red">';
                    echo $randnumber;
                    $randnumber++;
                    echo '</td>';
                }

                else
                {
                    echo '<td>';
                    echo $randnumber;
                    $randnumber++;
                    echo '</td>';
                }
            }

            echo '</tr>';
        }
        echo '</table>';

    }

    else
    {
        echo "<center>Rows / Columns / Highlight must ALL be INTEGER values. Re-enter correct value(s).</center>";
    }

    echo '<pre><center>';
    print_r($_POST);
    echo '</center></pre>';
}
?>

<style type ="text/css">
h1 {
    color: grey;
    text-align:center;
}

form {
    text-align: center;
    padding-bottom: 20px;
}

a:link {
    text-decoration: none;
}

.red {
    color: red;
}

.green {
    color: green;
}
</style>
</body>
</html>

So. I have this PHP code to generate a table based off the user's input and I recently ran into a problem I cant figure out how to fix.

It was working perfectly fine but now whenever I use the Refresh link it resets the entire page to default (i.e. default textbox values instead of keeping the current ones, removing the table).

So, I have 2 questions. How would I keep the data on refresh (with $_POST being used) and how to display the table with the default values when the page first loads.

  • 写回答

2条回答 默认 最新

  • dongwoqin7034 2015-10-05 00:19
    关注
    <a href="javascript:location.reload()">Refresh</a>
    

    Clicking it will trigger browser's reload mechanism and you'll be asked to resubmit the form action, it will allow you to keep POST data.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错