PHP扩展表与输入

时间:2015-03-14 03:06:43

标签: php html css

我的任务是写一个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>

1 个答案:

答案 0 :(得分:0)

这有两个部分很重要。首先,您应该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>