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

时间:2015-10-05 00:08:04

标签: php http-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>

因此。我有这个PHP代码根据用户的输入生成一个表,我最近遇到了一个问题,我无法弄清楚如何解决。

它工作得非常好,但现在每当我使用Refresh链接时,它会将整个页面重置为默认值(即默认文本框值而不是保留当前页面,删除表格)。

所以,我有2个问题。如何保持数据刷新(使用$ _POST)以及如何在页面首次加载时使用默认值显示表。

2 个答案:

答案 0 :(得分:0)

如果要保留参数,则需要重新创建帖子。可以通过循环遍历数组来完成。

<form method='POST' id='refresh' action='<?php echo $_SERVER['PHP_SELF']; ?>'>
<?php foreach($_POST as $k=>$v): ?>
<input type='hidden' name='<?php echo $k; ?>' value='<?php echo $v; ?>' />
<?php endforeach; ?>
<a href='#' onclick='document.getElementById("refresh").submit(); return false;'>refresh</a>
</form>

注意:这比其他答案稍长,但不会提示重新发送帖子数据。

答案 1 :(得分:0)

<a href="javascript:location.reload()">Refresh</a>

单击它将触发浏览器的重新加载机制,并且系统会要求您重新提交表单操作,它将允许您保留POST数据。

相关问题