Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/84.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
Html 如何仅在字段旁边显示php表单错误?_Html_Forms_Php - Fatal编程技术网

Html 如何仅在字段旁边显示php表单错误?

Html 如何仅在字段旁边显示php表单错误?,html,forms,php,Html,Forms,Php,我正在构建一个简单的PHP联系人表单,我的错误消息确实会以我喜欢的方式显示在每个表单字段的旁边,但它们也会出现在表单的顶部,我在HTML文件中编写了includes PHP代码。我想消除显示在includes代码顶部的错误。这是包含代码: <?php include("includes/form.inc.php"); ?> 这是我的html表单代码: <?php echo $finalMsg; ?> <form method="post" a

我正在构建一个简单的PHP联系人表单,我的错误消息确实会以我喜欢的方式显示在每个表单字段的旁边,但它们也会出现在表单的顶部,我在HTML文件中编写了includes PHP代码。我想消除显示在includes代码顶部的错误。这是包含代码:

<?php include("includes/form.inc.php"); ?>

这是我的html表单代码:

<?php echo $finalMsg; ?>

        <form method="post" action="contact.php">

            <fieldset>

                <ul>

                    <li class="instructions">(*) Required Fields</li>

                    <li>
                        <p><?php echo $errors["firstname"]; ?></p>
                        <label for="firstname" class="lined_up"><span class="asterisk">*</span> First Name:</label>
                        <input type="text" placeholder="" name="firstname" 
                                id="firstname" value="<?php echo $_POST["firstname"]; ?>" />
                                                      <!-- This keeps the content you enter in this field in place if you miss a field & have to resubmit-->
                    </li>

                    <li>
                        <p><?php echo $errors["lastname"]; ?></p>
                        <label for="lastname" class="lined_up"><span class="asterisk">*</span> Last Name:</label>
                        <input type="text" placeholder="" name="lastname" 
                            id="lastname" value="<?php echo $_POST["lastname"]; ?>" />
                    </li>

                    <li>
                        <p><?php echo $errors["email"]; ?></p>
                        <label for="email" class="lined_up"><span class="asterisk">*</span> Email:</label>
                        <input type="text" placeholder="Please type a valid email." name="email" 
                            id="email" value="<?php echo $_POST["email"]; ?>" />
                    </li>
                    <li>
                        <p><?php echo $errors["message"]; ?></p> 
                        <label for="message" class="message lined_up"><span class="asterisk">*</span> Message:</label>
                        <textarea id="message" name="message" placeholder="Please enter your message here."><?php echo isset($_POST['message'])? $_POST['message'] : ''; ?></textarea>

                    </li>
                    <li>
                        <input type="submit" value="Send" name="submit" id="submit" />
                    </li>
                </ul>
            </fieldset>

        </form>
<?php 

// 
if(isset($_POST["submit"]))
{

    //This is checking if the string length in the firstname field was greater than 1 character
    if(strlen($_POST["firstname"]) > 1)
    {

        $firstname = $_POST["firstname"];
        //echo $firstname;
    }

    else
    {
        //echo "You did not type in first name";
        $errors["firstname"] = "<span class=\"error\">You did not type in a first name.</span>";
    }


    if(strlen($_POST["lastname"]) > 1)
    {

        $lastname = $_POST["lastname"];
    }

    else
    {
        $errors["lastname"] = "<span class=\"error\">You did not type in a last name.</span>";
    }


    if(strlen($_POST["email"]) > 1)
    {

        $email = $_POST["email"];

        if(filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $email = $email;
        }

        else {
            $errors["email"] = "<span class=\"error\">Email is invalid.</span>";
        }
    }

    else
    {
        $errors["email"] = "<span class=\"error\">You did not type in an email address.</span>";
    }

    //This is checking if the string length in the message field was greater than 1 character
    if(strlen($_POST["message"]) > 1)
    {

        $message = $_POST["message"];
        //echo $firstname;
    }

    else
    {

        $errors["message"] = "<span class=\"error\">You did not type in a message.</span>";
    }


    // Code to tell form NOT to send form if there are any errors.
    if($errors < 1)
    {
            $to = "liz@lizshannon.com";
            $from = $email;
            // headers makes sure you have a reply-to address
            $headers = "From: {$from}" . "\r\n";
            $headers .= 'MIME-Version: 1.0' . "\r\n";
            $headers .= 'Content-type: text/html; charset=iso-8859-1';

            $subject =  
                "From: ($from)" . "<br />" .
                "First name: ($firstname)" . "<br />" .
                "Last name: ($lastname)" . "<br />" .
                "Email: ($email)" . "<br />" .
                "Message: ($message)";

            if(mail($to, $from, $subject, $headers))
            {
                $finalMsg = "<p class=\"success\">Thank you! Your email was sent.</p>";
            }

            else {
                $finalMsg = "<p class=\"error\">Your email was NOT sent. Please try again.</p>";
            }

}

?>

<?php

//each error is displayed

foreach($errors as $value) {

echo "<span>$value</span><br />";

}
    }
?>

  • (*)必填字段
  • *名字:
    从中删除echo,或者您可以删除整个for循环块,因为您现在不想打印它

    foreach($errors as $value) {
    
    echo "<span>$value</span><br />";
    
    }
    
    foreach($errors作为$value){
    回显“$value
    ”; }

    您应该在客户端使用jquery或javascript验证,这样可以减少服务器-客户端通信的时间

    只需删除以下代码块:

    <?php
      //each error is displayed
       foreach($errors as $value) {
        echo "<span>$value</span><br />";
        }
    }
    

    使用jquery验证程序plugins进行客户端检查很难从您的问题中分辨出来,但我假设它们显示为PHP错误。添加
    显示错误(false)位于代码顶部。如果您可以访问服务器上的php配置,最好在php.ini中关闭它们。它应始终在生产现场关闭。您不应完全依赖客户端验证。这将打开一个巨大的安全漏洞。@LoganBailey是的,但你应该检查客户端和服务器端以获得更多的安全性。我告诉过添加客户端而不是删除服务器端。@yadav chetan:双方验证的好处是什么,客户端验证和服务器验证是否有可能产生不同的结果?客户端验证的好处是,您不需要每次都与服务器通信。它只会通过浏览器进行检查,以便快速执行代码。但您也必须放置服务器端,因为如果浏览器中禁用了javascript,则浏览器不会检查客户端验证