带有输入的 PHP 扩展表


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>