为表单中的每个输入回显错误消息

时间:2013-09-07 21:59:34

标签: php forms

我有一个表格,上面有姓名和电子邮件的输入。我想为每个输入--fx提供专用的错误消息 如果没有写出名字,它会说"please write your name!" 如果电子邮件丢失或无效,则回复"wrong mail - try again!"

目前我只有一条错误信息会在两种情况下都回应。 如何为每个输入分配专用的错误消息?

下面是代码:

    <?php
//If the form is submitted
if(isset($_POST['submit'])) {

    //Check to make sure that the name field is not empty
    if(trim($_POST['contactname']) == '') {
        $hasError = true;
    } else {
        $name = trim($_POST['contactname']);
    }

    //Check to make sure sure that a valid email address is submitted
    if(trim($_POST['email']) == '')  {
        $hasError = true;
    } else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) {
        $hasError = true;
    } else {
        $email = trim($_POST['email']);
    }

    //If there is no error, send the email
    if(!isset($hasError)) {
        $emailTo = '#@gmail.com'; //Put your own email address here
        $body = "Name: $name \n\nEmail: $email \n\nComments:\n $comments";
        $headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;

        mail($emailTo, $subject, $body, $headers);
        $emailSent = true;
    }
}
?>


    <article class="kontakt">                    
         <?php if($_POST['contactname'] != '') { //echo when a name was entered ?> 
                <!--<p> Hello </p>-->
         <?php  $name = strip_tags(trim($_POST['contactname']));
               echo  $name; 
          } ?>  
    </article>


    <article class="kontakt">   

         <?php if(isset($hasError)) { // THIS PART IS ECHOED IN BOTH SITUATIONS - should only apply for error in e-mail?>
            <p class="error"> Your mail is <span style="color: orange"> not correct</span> - try again! </p>
        <?php } ?>  


        <?php if($_POST['email'] != '') { // echo when a valid mail was entered ?> 
              <p> Hello </p>
        <?php   $name = strip_tags(trim($_POST['email']));
             echo  $email; 
        } ?>     
    </article>

    <article class="kontakt">  
        <?php if(isset($emailSent) && $emailSent == true) { //If email is sent ?>
            <p> Your message is sent !</p>
        <?php } ?>       
    </article>

}
?>

3 个答案:

答案 0 :(得分:0)

您使用单独的代码处理每个输入,这意味着您没有太多的数据结构。

通常情况下,输入的某种模型以及相关的错误和值很有用来完成表单。

通常适用于单个PHP脚本的轻量级条目是HTML_QuickForm2

它也会处理个别错误消息。您自然可以自己编写,幸运的是它是免费软件,因此您可以学习代码并从中学习。

答案 1 :(得分:0)

尝试类似:

if(trim($_POST['contactname']) == '') {
    $hasError['contactname'] = 'Please enter a contact name!';
} else {
    $name = trim($_POST['contactname']);
}

//Check to make sure sure that a valid email address is submitted
if(trim($_POST['email']) == '')  {
    $hasError['email'] = 'Please enter email!';
} else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) {
    $hasError['validemail'] = 'Please enter valid email';
} else {
    $email = trim($_POST['email']);
}

这将显示错误:

if(isset($hasError)){
    echo '<ul>';
    foreach($hasError as $error){
        echo '<li>'.$error.'</li>';
    }
    echo '</ul>';
}

这是为了在出错时保留旧值

<div class="right_wrap">
<div class="header"> <h5><span style="color: white"> mail </span></h5> </div>
    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" id="contactform">

        <article class="kontakt">
            <label for="name"> Dit navn </label>
            <input type="text" name="contactname" id="contactname" value="<?php echo $_POST['contactname']; ?>" class="" />
        </article>

        <article class="kontakt">
            <label for="email"> Din mail </label>
            <input type="text" name="email" id="email" value="<?php echo $_POST['email']; ?>" class="required email" />
        </article> 

        <article class="kontakt" style="height: auto">
            <label for="message"> Din besked </label>

            <textarea rows="5" cols="50" name="message" class="required"><?php echo $_POST['message']; ?></textarea>
        </article>

        <article class="kontakt">
            <input type="submit" value="Send besked" name="submit" class="button"/>
        </article>
    </form>          
</div> <!--end of right_wrap -->

答案 2 :(得分:0)

这是实际形式 - 其中有一些丹麦语。无论在左包装div中输出什么错误/消息 - 都不能放在实际表单中。

<div class="right_wrap">
<div class="header"> <h5><span style="color: white"> mail </span></h5> </div>
    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" id="contactform">

        <article class="kontakt">
            <label for="name"> Dit navn </label>
            <input type="text" name="contactname" id="contactname" value="" class="" />
        </article>

        <article class="kontakt">
            <label for="email"> Din mail </label>
            <input type="text" name="email" id="email" value="" class="required email" />
        </article> 

        <article class="kontakt" style="height: auto">
            <label for="message"> Din besked </label>

            <textarea rows="5" cols="50" name="message" class="required"></textarea>
        </article>

        <article class="kontakt">
            <input type="submit" value="Send besked" name="submit" class="button"/>
        </article>
    </form>          
</div> <!--end of right_wrap -->