【问题标题】:php validation removes the data from html inputphp 验证从 html 输入中删除数据
【发布时间】:2015-11-30 01:28:08
【问题描述】:

我在使用 PHP 进行 html 输入验证时遇到了一些问题。验证本身正在工作。我有两个输入:姓名和办公室。 如果我在名称输入中输入了一个值,但我没有在办公室输入中输入值并单击提交按钮,则对办公室输入的验证有效,但它清除/清空了我在名称输入中输入的数据

我在这里做错了什么?

这是我的 PHP 验证:

if (isset($_POST['submit'])){
    $signatory_name = $_POST['sig_name'];
    $signtory_position = $_POST['sig_position'];

    if (!$_POST['sig_name']) {
            $errname='<div class="alert alert-danger">Sorry there was an error: Please Enter Your Name</div>';
        }

    if (!$_POST['sig_office']) {
            $erroffice='<div class="alert alert-danger">Sorry there was an error: Please Enter Your office</div>';
        }
}

这是我的html代码

<form action="signatory.php" method="Post" role="form">
<input class="form-control " id="signatoryname" name="sig_name" placeholder="Name:" >
<input class="form-control " id="signatoryoffice" name="sig_office" placeholder="Office:">
</form>

【问题讨论】:

  • 验证失败后如何重新填充表单尚不清楚。

标签: php html validation


【解决方案1】:

不清楚你在做什么或试图做什么,但这是我的尝试:

首先:您应该知道if (!$_POST['sig_name']) { 表示如果分配的值为 FALSE,您可能需要重新考虑这一点并改用empty()

验证输入后,您需要使用提交的值重新填充表单 - 这是一个示例:

<?php

    $errname = "";
    $erroffice= "";
    if (!empty($_POST)) {   // Only if there are POST values attached.

        $signatory_name = $_POST['sig_name'];
        $signtory_position = $_POST['sig_position'];

        if (empty($_POST['sig_name'])) {
            $errname='<div class="alert alert-danger">Sorry there was an error: Please Enter youre Name</div>';
        }

        if (empty($_POST['sig_office'])) {
             $erroffice='<div class="alert alert-danger">Sorry there was an error: Please Enter youre office</div>';
        }
        if (empty($errname) && empty($erroffice)) {

             //Do whatever you need with the validated inputs...

        } else {
            //Expose the alerts:
            echo $errname.$erroffice;
        }
    }
?>


<form method="POST" role="form">
    <input class="form-control" id="signatoryname" name="sig_name" value="<?php echo (isset($_POST['sig_name']))?$_POST['sig_name']:""; ?>" placeholder="Name:" />
    <input class="form-control" id="signatoryoffice" name="sig_office" value="<?php echo (isset($_POST['sig_office']))?$_POST['sig_office']:""; ?>" placeholder="Office:" />

    <!-- rest of your form and buttons -->
</form>

【讨论】:

    【解决方案2】:

    您需要按照 Scuzzy 所说的重新填写表单。

    大多数浏览器可能会帮你,但你不能依赖它。

    <form action="signatory.php" method="Post" role="form">
    <input class="form-control " id="signatoryname" name="sig_name" placeholder="Name:" value="<?php echo !empty($_POST['sig_name']) ? $_POST['sig_name'] : ''; ?>">
    <input class="form-control " id="signatoryoffice" name="sig_office" placeholder="Office:" value="<?php echo !empty($_POST['sig_office']) ? $_POST['sig_office'] : ''; ?>">
    </form>
    

    【讨论】:

    • 同时删除这个 sn-p 框,它没有做任何事情,只是令人困惑。
    • 您不会收到未定义的索引通知,因为它包裹在 !empty 三元语句中。如果为空则输出一个空字符串。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-21
    • 2014-03-27
    • 2016-11-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多