<!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代碼基於用戶的輸入生成一個表,我最近遇到了一個問題,我不知道如何解決。
這是工作完全正常,但現在每當我使用刷新鏈接將會重置整個頁面爲默認(即文本框的默認值,而不是保持當前的,刪除表)。
所以,我有2個問題。我將如何保持數據刷新(使用$ _POST)以及如何在首次加載頁面時使用默認值顯示錶格。
POST之後,當你刷新,瀏覽器會詢問您是否要重新發送數據。如果您單擊重新發送,它將刷新第一次POST發送的數據。 –
Cookie或會話?客戶端替代品可以是本地存儲。 Ofcourse您W「on'T使用'$ -_ POST'關於句子中的隨機錯誤道歉,看來我的鍵盤是在我放棄。 – Script47
是的,當我刷新它的工作頁面,但是,我想使用刷新按鈕刷新頁面,點擊刷新按鈕即可重新設置它,好像加載的頁面第一次編輯 - 。還沒有學會使用Cookie或會話我的代碼是和它之前 – James