Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/235.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP表单验证无法正常运行_Php_Html - Fatal编程技术网

PHP表单验证无法正常运行

PHP表单验证无法正常运行,php,html,Php,Html,我的联系方式有点问题。我想我一切都对,但我总是得到同样的错误结果 这是我的密码: <?php if (isset($_POST['contact_name']) && isset($_POST['contact_email']) && isset($_POST['contact_message'])) { $contact_name = $_POST['contact_name']; $contact_emai

我的联系方式有点问题。我想我一切都对,但我总是得到同样的错误结果

这是我的密码:

    <?php
    if (isset($_POST['contact_name']) && isset($_POST['contact_email']) && isset($_POST['contact_message'])) {
        $contact_name = $_POST['contact_name'];
        $contact_email = $_POST['contact_email'];
        $contact_message = $_POST['contact_message'];

        if (!empty($contact_name) && !empty($contact_email) && !empty($contact_message)) {
            echo '0K!';
        } else {
            echo 'Every field is required.';
        }
    }
?>          
        <form action="contacts.php" method="POST">
            Nome:<br><input type="text" name="contact_name" maxlength="25"><br><br>
            Email:<br><input type="text" name="contact_email" maxlength="50"><br><br>
            Mensagem:<br>
            <textarea name="contact_message" rows="6" cols="30" maxlength="1000"></textarea><br><br>
            <input type="submit" value="Enviar">
        </form>

名称:


电子邮件:


菜单:


当我填写所有字段时,我得到相同的结果“每个字段都是必需的”。当它应该显示“OK!”。但是我知道所有的字段都收到了我写的东西,因为我之前测试过


我知道这是一个基本问题,但我无法解决。

如果是多余的,那么您的外部
。这些仅在POST操作期间设置,但无论是否完整,都将为每个POST设置,因为表单元素始终提供值。所以,只需使用这个:

if ($_POST) {
接下来,如果索引不存在,则以不会引发警告的方式读取值:

$contact_name = isset($_POST['contact_name']) ? $_POST['contact_name'] : null;
$contact_email = isset($_POST['contact_email']) ? $_POST['contact_email'] : null;
$contact_message = isset($_POST['contact_message']) ? $_POST['contact_message'] : null;
然后,您可以像这样进行简单的测试:

if ($contact_name && $contact_email && $contact_message) {
     // ok
}
试试这个:

<?php
if(isset($_POST['submit'])) {
    $contact_name = $_POST['contact_name'];
    $contact_email = $_POST['contact_email'];
    $contact_message = $_POST['contact_message'];

    if (empty($contact_name) && empty($contact_email) && empty($contact_message)) {
        echo 'Every field is required.'; //This would be the message if the field(s) are empty.
    } else {
        echo 'OK!'; //This would be the message if all the fields are filled in.
    }
}
?>          
    <form action="contacts.php" method="POST">
        Nome:<br><input type="text" name="contact_name" maxlength="25"><br><br>
        Email:<br><input type="text" name="contact_email" maxlength="50"><br><br>
        Mensagem:<br>
        <textarea name="contact_message" rows="6" cols="30" maxlength="1000"></textarea><br><br>
        <input type="submit" value="Enviar" name="submit">
    </form>

名称:


电子邮件:


菜单:



错误是:
!空($contact\u mensage)
(消息已由halfer编辑),但实际上您有
$contact\u消息
。 我已经在我的计算机上尝试了该代码,它成功了:

 <?php
    if (isset($_POST['contact_name']) && isset($_POST['contact_email']) && isset($_POST['contact_message'])) {
        $contact_name = $_POST['contact_name'];
        $contact_email = $_POST['contact_email'];
        $contact_message = $_POST['contact_message'];

        if (!empty($contact_name) && !empty($contact_email) && !empty($contact_message)) {
            echo '0K!';
        } else {
            echo 'Every field is required.';
        }
    }
?>          
        <form action="contacts.php" method="POST">
            Nome:<br><input type="text" name="contact_name" maxlength="25"><br><br>
            Email:<br><input type="text" name="contact_email" maxlength="50"><br><br>
            Mensagem:<br>
            <textarea name="contact_message" rows="6" cols="30" maxlength="1000"></textarea><br><br>
            <input type="submit" value="Enviar">
        </form>

名称:


电子邮件:


菜单:



我认为您的条件自相矛盾。外部的
if
应该是
if($\u POST){
-看起来您正在检查内部的
if
是否完整。但是这样,我就无法知道用户是否正确填写了所有字段(如果你想在这里与某人交谈,请在他们的用户名前加上如下前缀:@halfer。我不必这样做,因为你自己的帖子上总是会有评论通知你)。我想我已经发现了你的问题,等等…
!empty
表示
不为空