0

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

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

继承人的代码:

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

}
?>
4

3 回答 3

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 -->
于 2013-09-07T23:27:45.893 回答
0

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

通常,某种输入模型以及相关的错误和值有助于使用表单完成工作。

一个通常与单个 PHP 脚本配合良好的轻量级条目是HTML_QuickForm2.

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

于 2013-09-07T22:08:02.177 回答
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 -->
于 2013-09-07T22:09:02.707 回答