繁体   English   中英

以表格形式保存PHP值

[英]Preserving PHP values in a form

如果发生错误,我在保留写入文本字段内的值时会遇到问题。 我有4个文本字段,如果1为空白,则需要显示一个新表单,并带有错误消息和上一个文件在文本字段中的输入。

我想这是我的Assignment_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

?>

assigment_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>

我可能会先处理客户端验证,因此直到所有输入都填满后才提交表单,然后再进行服务器端验证和清理。 顺便说一句,你不需要Assigment2。

保持简单! 对于初学者,请尝试仅处理一个文件,然后将错误放入数组中。

然后尝试缩短您的代码,并且不要“复制并粘贴”代码。

在现代站点上,开发人员使用框架来验证表单,继续使用这一框架,直到它可以按您希望的方式工作为止,并查看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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM