[英]PHP expanding table with input
我的任務是編寫一個PHP腳本以在表中顯示字符串和值。 當用戶輸入新信息時,表格將擴展為包括該信息。 當我運行此腳本時,它將覆蓋現有信息。 我只能使用PHP。 我看過其他使用jQuery和JavaScript的示例。
表格的內部邊框也不會顯示。 如果有人可以教我如何解決問題,那就太好了。 謝謝。
<!Doctype html>
<html>
<body>
<style>
table {
border: 1px solid black;
}
</style>
<?php
$err = "";
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Please enter a name: <input type="text" name="string"><br/>
Please enter in a amount in $: <input type="text" name="number"><br/>
<input type="submit" name="submit" value="Submit"><br/>
<span><?php echo $err;?></span></br><br/>
</form>
<?php
echo "<table>";
$err = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["string"]) || empty($_POST["number"]) ) {
$err = "Please fill both fields.";
} else {
$name = $_POST["string"];
$number = $_POST["number"];
echo "<tr><td> Salary of $name is <td/><td> $number <td/><tr>";
}
}
echo "</table>";
?>
</br><br/>
<a href="Assignment5-php.html">Back</a>
</body>
</html>
這有兩個很重要的部分。 首先,您應該array
表單輸入,然后添加hidden
輸入,並循環遍歷表單以存儲下一次添加的數據。 此解決方案不需要$_SESSION
:
<!Doctype html>
<html>
<body>
<style>
/* You need to add the borders to the TDs as well */
table {
border: 1px solid black;
}
td:first-child {
border-right: 1px solid black;
}
</style>
<?php
$err = "";
// I am making this a function, but you can split this into
// separate foreach loops in the two spots
function LoopPost($type = 'form')
{
// I've made no allowance for empty values, you can write that part
if(isset($_POST['number'])) {
// Loop through the posts to either display form inputs
// or render the table rows
foreach($_POST['number'] as $key => $value) {
if($type == 'form') { ?>
<input type="hidden" name="string[]" value="<?php echo htmlspecialchars($_POST['string'][$key], ENT_QUOTES); ?>" />
<input type="hidden" name="number[]" value="<?php echo htmlspecialchars($value, ENT_QUOTES); ?>" />
<?php }
else { ?>
<tr>
<td>Salary of <?php echo htmlspecialchars($_POST['string'][$key], ENT_QUOTES); ?> is</td>
<td><?php echo htmlspecialchars($value, ENT_QUOTES); ?></td>
</tr>
<?php }
}
}
}
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<!-- Array these inputs. It will store multiple key/value pairs -->
Please enter a name: <input type="text" name="string[]"><br/>
Please enter in a amount in $: <input type="text" name="number[]"><br/>
<?php LoopPost(); // Default is to render form ?>
<input type="submit" name="submit" value="Submit"><br/>
<!-- Just a reminder, I've made no validation checks -->
<span><?php echo $err;?></span></br><br/>
</form>
<table>
<?php
$err = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["string"]) || empty($_POST["number"]) )
$err = "Please fill both fields.";
else
// You can fill this function with any value except false or empty
// Empty/false will render the form inputs
LoopPost('table');
} ?>
</table>
</br><br/>
<a href="Assignment5-php.html">Back</a>
</body>
</html>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.