在表单中保留PHP值


Preserving PHP values in a form

如果发生错误,我在保存写在文本字段中的值时遇到问题。我有4个文本字段,如果1为空,它需要显示一个新表单,其中包含一条错误消息和上一个文件的文本字段中的输入。

我想这是我作业_2.php的最后一部分,它错了。

assignment_1.php

   <!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <form action="sendit.php" method="get">
            <input type="text" name="name" placeholder="name"/>
            <br>
            <input type="text" name="adress" placeholder="adress"/>
         <br>
            <input type="text" name="city" placeholder="city"/>      
         <br>
            <input type="text" name="zip" placeholder="zip"/>
             <br>
            <input type="submit" />
        </form>
        <br>
    </body>
</html>

sendit.php

<?php
$name = $_GET['name'];
$adress = $_GET['adress'];
$city = $_GET['city'];
$zip = $_GET['zip'];
if (!isset($_GET['name']) || $_GET['name'] == '') {
    header("Location: assignment_2.php?errmsg=1");
    exit;
}
else {
    header("Location: assignment_2.php?errmsg=1&name=$name");
}
if (!isset($_GET['adress'])|| $_GET['adress'] == '') {
    header("Location: assignment_2.php?errmsg=2&adress=$adress");
    exit;
}    
else {
    header("Location: assignment_2.php?errmsg=1&adress=$adress");
}
if (!isset($_GET['city'])|| $_GET['city'] == '') {
    header("Location: assignment_2.php?errmsg=3&city=$city");
    exit;
}  
else {
    header("Location: assignment_2.php?errmsg=1&city=$city");
}
if (!isset($_GET['zip'])|| $_GET['zip'] == '') {
    header("Location: assignment_2.php?errmsg=4&zip=$zip");
    exit;
}  
else {
    header("Location: assignment_2.php?errmsg=4&zip=$zip");
}
echo $name . "<br>" . $adress . "<br>" . $city . "<br>" . $zip
?>

assignment_2.php

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <?php
        // 1.0  Create a contactform containing name, address, city, zipcode
        //      Send it to a form handler
        //      If any of the form fields are not filled out, return to this page and 
        //      display an error containing information on how to prevent the error
        // 1.1  Preserve the input for the user
        ?>
        <?php

            if (isset($_GET['errmsg'])) {
                $err = $_GET['errmsg'];
                switch ($err) {
                    case 1:
                        $err_msg = 'Missing navn';
                        break;
                    case 2:
                        $err_msg = 'Missing adress';
                        break;
                    case 3:
                        $err_msg = 'Missing city';
                        break;
                    case 4:
                        $err_msg = 'missing zip';
                        break;
                    default: 
                        $err_msg = 'I just dont like you';   
                        break;
                }
                echo '<div class="error">' . $err_msg . '</div>';
            }
        ?>
        <form action="sendit.php" method="get">
            <input type="text" name="name" placeholder="name" <?php
                if (isset($_GET['name'])) echo 'value="' .$_GET['name'] .'"';
            ?> />
            <br>
            <input type="text" name="adress" placeholder="adress" <?php
                if (isset($_GET['adress'])) echo 'value="' .$_GET['adress'] .'"';
            ?>/>
         <br>
            <input type="text" name="city" placeholder="city" <?php
                if (isset($_GET['city'])) echo 'value="' .$_GET['city'] .'"';
            ?>/>      
         <br>
            <input type="text" name="zip" placeholder="zip" <?php
                if (isset($_GET['zip'])) echo 'value="' .$_GET['zip'] .'"';
            ?>/>
             <br>
            <input type="submit" />
        </form>
    </body>
</html>

我可能会处理第一个客户端验证,所以在所有输入都填写好之前,表单不会提交,然后我会进行一些服务器端验证和清理。顺便说一句,你不需要有任务。

保持简单!对于初学者来说,试着只处理一个文件,并将错误放入一个数组中。

然后试着缩短你的代码,并且永远不要"复制和粘贴"代码。

在现代网站上,开发人员使用框架来验证表单,继续玩这个,直到它像你想要的那样工作,然后看看Symfony或Zend Framework表单验证。

<?php
$errors = array();
if (isset($_GET['submitted'])) {
    if (!isset($_GET['name']) || $_GET['name'] == '')
        $errors[] = 'Missing navn'
    if (!isset($_GET['adress']) || $_GET['adress'] == '')
        $errors[] = 'Missing navn'
    if (!isset($_GET['city']) || $_GET['city'] == '')
        $errors[] = 'Missing navn'
    if (!isset($_GET['zip']) || $_GET['zip'] == '')
        $errors[] = 'Missing navn'
}
?><!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <?php
            if (count($errors) !== 0)
                echo '<div class="error">' . implode("<br>", $errors) . '</div>';
        ?>
        <form action="" method="get">
            <input type="hidden" name="submitted" value="1" />
            <input type="text" name="name" placeholder="name" value="<?php echo isset($_GET['name']) ? $_GET['name'] : '' ?>" />
            <br>
            <input type="text" name="adress" placeholder="adress" value="<?php echo isset($_GET['adress']) ? $_GET['adress'] : '' ?>" />
         <br>
            <input type="text" name="city" placeholder="city" value="<?php echo isset($_GET['city']) ? $_GET['city'] : '' ?>" />      
         <br>
            <input type="text" name="zip" placeholder="zip" value="<?php echo isset($_GET['zip']) ? $_GET['zip'] : '' ?>" />
             <br>
            <input type="submit" />
        </form>
        <br>
    </body>
</html>