在另一个文件中显示错误消息

时间:2015-03-10 14:09:09

标签: php html

我很抱歉新手问题,但我真的无法弄清楚如何做到这一点。所以首先我有联系表格,它在模态窗口中。联系表单的服务器端( PHP端)位于另一个文件contact.php中。在这个文件中,我对字段进行了一些验证。我想要的是当存在空字段或错误输入以在表单上显示错误消息时。现在正在加载contact.php,错误就在那里。 这是contact.php

验证的部分
  function died($error) {
    echo "We are very sorry, but there were error(s) found with the form you submitted. ";
    echo "These errors appear below.<br /><br />"; 
    echo $error."<br /><br />"; 
    echo "Please go back and fix these errors.<br /><br />";
    die(); 
}
 // validation expected data exists 
if(!isset($_POST['firstname']) || 
    !isset($_POST['lastname']) ||
    !isset($_POST['email']) || 
    !isset($_POST['phone']) || 
    !isset($_POST['description'])) { 
    died('We are sorry, but there appears to be a problem with the form you submitted.');       
} 
$firstname = $_POST['firstname']; // required 
$lastname = $_POST['lastname']; // required 
$email = $_POST['email']; // required 
$phone = $_POST['phone']; // not required 
$description = $_POST['description']; // required 
$error_message = ""; 
 $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/'; 
if(!preg_match($email_exp,$email)) { 
   $error_message .= 'The email address is not valid!<br />'; 
} 
$string_exp = "/^[a-zA-Z\p{Cyrillic}0-9\s\-]+$/u"; 
if(!preg_match($string_exp,$firstname)) {
   $error_message .= 'The name is not valid.<br />';
}
if(!preg_match($string_exp,$lastname)) { 
   $error_message .= 'The last name is not valid.<br />'; 
}
if(strlen($description) < 2) {
   $error_message .= 'The field for description is not valid.<br />'; 
} 
if(strlen($error_message) > 0) { 
   died($error_message);
} 
...

和下面的html表单

  <div class="send-icon" data-toggle="modal" data-target="#contact-form">
        <i class="fa fa-paper-plane"></i>
    </div>
    <p class="light-text">Contact Us</p>

    <!-- Contact Form Modal -->
    <div class="modal fade contact-form" id="contact-form" tabindex="-1" role="dialog" aria-labelledby="contact-form" aria-hidden="true">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">×</span>
                </button>
                <div class="modal-body">    
  <!-- *****  Contact form ***** -->
  <form class="form" name="contactform" action="contact/contact.php" method="post">
  <div class="row">
   <div class="form-group col-md-6">
       <input type="text" class="form-control" id="firstname" name="firstname" placeholder="firstname">
   </div>
   <div class="form-group col-md-6">
       <input type="text" class="form-control" id="lastname" name="lastname" placeholder="lastname">
   </div>
   <div class="form-group col-md-6">
       <input type="email" class="form-control" id="email" name="email" placeholder="Email" >
   </div>
   <div class="form-group col-md-6">
        <input type="text" class="form-control" id="phone" name="phone" placeholder="Phone" >
   </div>             
   <div class="form-group col-md-12 mab-none">
        <textarea rows="6" class="form-control" id="description" name="description" placeholder="description" ></textarea>
   </div>
   <div class="form-group col-md-12">
        <button class="button bold-text main-bg" type="submit" ><i class="fa fa-paper-plane"></i></button>
   </div>
 </div>
</form>

再次原谅我的问题是否是#34;太新手&#34;对于你们,但我真的需要一些帮助。

1 个答案:

答案 0 :(得分:0)

假设您的代码与this tutorial完全相同(正如您在评论中所说):

你有什么:

所以目前您的表单在index.php

<!-- HTML stuff before -->

<form class="form" name="contactform" action="contact.php" method="post">
    <!-- Input tags -->
</form>

<!-- HTML stuff after -->

表单在contact.php

中处理
if(isset($_POST['email'])) {
     function died($error) {
         echo 'You have an error blahblah.';
         echo $error;
         die();
     }

     /*
         Tests to validate the values
         Puts some strings in $error_message if not valide
     */

     if (strlen($error_message) > 0) {
         died($error_message);
         // If there is an error, call the function that displays the message and stops everything
     }

     // Send email
}

我的建议:

最好的方法是在同一页面中同时使用HTML和PHP(就像它在同一个文件中一样)。在index.php开头使用include()

<?php
    include('contact.php');
?>

<!-- HTML stuff before -->

<?php
    if (strlen($error_message) > 0) {
        // Now we can display our error message here, with the HTML.
        echo '<div class="error">There was an error: '.$error_message.'</div>';
    }
?>

<form class="form" name="contactform" action="index.php" method="post">
    <!-- See the "action" value has changed, as we won't go to a new "contact" page but stay here -->
</form>

<!-- HTML stuff after -->

然后contact.php文件必须稍微更新。

if(isset($_POST['email'])) {
// Notice here that the code won't be executed if the form has not been submitted: so it is not a problem to include the code before the HTML.

     // We don't used the died() function anymore: delete it.

     /*
         That does not change:
         Tests to validate the values
         Puts some strings in $error_message if not valide
     */

     if (strlen($error_message) == 0) {
         // Send the mail only if there is no error.

         // Send email
     }
}

当然这不是一个完美的联系页面(例如,我没有将错误存储在一个简单的$error_message字符串中),但这是从PHP和HTML开始的一个良好的开端并且理解这个怎么运作。我建议您在理解了此过程后更新并修改此代码。

*编辑:我看到了你对模态的评论。当重新加载页面时,此代码不会打开模式,您仍然需要单击按钮。
如果strlen($error_message) > 0,您必须强制模式的CSS样式显示它(style="display:block"需要的地方)。
或者用JS打开它(我假设你在某个地方有一个函数.modal().open()来强制它打开。)。 (不是我最喜欢的解决方案,好像加载有点慢,用户在打开之前会看到没有模态的页面。)