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 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件