PHP错误输出我的html布局的擦除部分

时间:2016-06-24 04:03:06

标签: php forms

我借用了如何编写PHP电子邮件的代码。但是,而不是他们的:http://www.freecontactform.com/email_form.php我决定使用$PHP_SELF并在一个文件中完成所有操作。

然而,由于错误输出,它正在删除我的联系我们的东西,表格和页脚的东西。

所以我想,一定是个故障,我只是回应联系我们,表格和页脚中的内容,并将错误信息放在表格末尾和页脚之间。

然而,令我烦恼的是,它仍在消除它,并且表现得像我的新东西甚至不存在。为什么这样,我该怎么做才能解决它?我最好像他们一样使用两个单独的文件吗? (我不喜欢他们的方法,因为一旦他们输入正确或即使他们输错了也没有后退按钮。)

更奇怪的是,如果我确实做到了并且不做错误消息,它确实会输出上面的所有内容,虽然在底部而不是在我原来的形式和形式的位置。

最后,当我查看我的电子邮件时(我知道上面还有其他内容,但在我的主电子邮件中,我使用了我的电子邮件地址,并且也将它放在了来自框中。但是,它没有显示在我的电子邮件中邮箱。

<html>
    <head>
        <title> Contact Us </title>
        <link rel="stylesheet" type="text/css" href="./main.css">
        <link rel="icon" href="./ski_icon.png">
        <script src="jquery-3.0.0.min.js" type="text/javascript"></script>
        <script>
            $( document ).ready(function() {
                $("#container").css("height", 1135);
            });
        </script>
    </head>
    <body>
        <style>
            form {
                border: 1px solid transparent;
                text-align: center;
            }
        </style>
        <div id="content">
            <div id="logo">
                <img src="./logo.png"></img>
            </div>
            <div id="container">
                <nav id="hor">
                    <div><a href="index.html"> Home </a> </div>
                    <div><a href="mountains.html"> The Mountains </a></div>
                    <div><a href="resorts.html"> The Resorts </a></div>
                    <div><a href="snowreports.html"> Snow Reports </a></div>
                    <div><a href="eventsandactivities.html"> Events &amp Activities </a></div>
                    <div><a href="plantrip.html"> Plan A Trip </a></div>
                </nav>
                <div id="middle">
                    <nav id="vert">
                        <a href="index.html"> Home </a> <br/>
                        <a href="mountains.html"> The Mountains </a><br/>
                        <a href="resorts.html"> The Resorts </a><br/>
                        <a href="snowreports.html"> Snow Reports </a><br/>
                        <a href="eventsandactivities.html"> Events &amp Activities </a><br/>
                        <a href="plantrip.html"> Plan A Trip </a>
                    </nav>    
                    <style>
                        td {
                            vertical-align: top;
                        }
                    </style>    
                    <?php
                        if(isset($_POST['email'])) {
                            // EDIT THE 2 LINES BELOW AS REQUIRED
                            $email_to = "you@yourdomain.com";
                            $email_subject = "Your email subject line";
                            function died($error) {
                                // your error code can go here
                                echo "<div id='text-container'>" ;
                                    echo "<h1>Contact Us</h1>";
                                    echo "<form name='contactform' method='post' action='$_PHP_SELF'>";
                                        echo"<table width='450px'>";
                                            echo "<tr>";
                                                echo" <td valign='top'>";
                                                    echo "<label for='first_name'>First Name *</label>";
                                                echo "</td>";
                                                echo " <td valign='top'>";
                                                    echo" <input  type='text' name='first_name' maxlength='50' size='30'>";
                                                echo "</td>";
                                            echo "</tr>";
                                            echo "<tr>";
                                                echo"<td valign='top'>";
                                                    echo "<label for='last_name'>Last Name *</label>";
                                                echo "</td>";
                                                echo "<td valign='top'>";
                                                    echo "<input  type='text' name='last_name' maxlength='50' size='30'>";
                                                echo "</td>";
                                            echo "</tr>";
                                            echo "<tr>";
                                                echo "<td valign='top'>";
                                                    echo "<label for='email'>Email Address *</label>";     
                                                echo "</td>";
                                                echo "<td valign='top'>";
                                                    echo "<input  type='text' name='email' maxlength='80' size='30'>";
                                                echo "</td>";
                                            echo "</tr>";
                                            echo "<tr>";
                                                echo "<td valign='top'>";
                                                    echo "<label for='telephone'>Telephone Number</label>";
                                                echo "</td>";
                                                echo "<td valign='top'>";
                                                    echo "<input  type='text' name='telephone' maxlength='30' size='30'>";
                                                echo "</td>";
                                            echo "</tr>";     
                                            echo "<tr>";
                                                echo "<td valign='top'>";
                                                    echo  "<label for='comments'>Comments *</label>";
                                                echo "</td>";
                                                echo "<td valign='top'>";
                                                    echo "<textarea  name='comments' maxlength='1000' cols='25' rows='6'></textarea>";
                                                echo "</td>";
                                            echo "</tr>";
                                            echo "<tr>";
                                                echo "<td colspan='2' style='text-align:center'>";
                                                    echo "<input type='submit' value='Submit'>   <a href='http://www.freecontactform.com/email_form.php'>Email Form</a>";     
                                                echo "</td>";
                                            echo "</tr>";
                                        echo "</table>";
                                    echo "</form>";
                                    echo "Bla bla bla";
                                    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 />";
                                echo "</div>";
                            echo "</div>";
                        echo "</div>";
                        echo "<div id='footercontainer'>";
                            echo "<footer>";
                                echo "Copyright  &copy;  2016   Dimaond Peaks Resort, &nbsp; All Rights Reserved.  <br/>";
                                echo "<a href='http://www.allwebpromotion.com/' target='_blank'>Website Design</a> by: All Web Promotion";
                            echo "</footer>";
                        echo "</div>";
                    echo "</div>";
                }
                // validation expected data exists
               if(!isset($_POST['first_name']) || !isset($_POST['last_name']) || !isset($_POST['email']) || !isset($_POST['telephone']) || !isset($_POST['comments'])) 
               {
                   died('We are sorry, but there appears to be a problem with the form you submitted.');       
               }
               $first_name = $_POST['first_name']; // required
               $last_name = $_POST['last_name']; // required
               $email_from = $_POST['email']; // required
               $telephone = $_POST['telephone']; // not required
               $comments = $_POST['comments']; // required
               $error_message = "";
               $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
               if(!preg_match($email_exp,$email_from)) {
                   $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
               }
               $string_exp = "/^[A-Za-z .'-]+$/";
               if(!preg_match($string_exp,$first_name)) {
                   $error_message .= 'The First Name you entered does not appear to be valid.<br />';
               }
               if(!preg_match($string_exp,$last_name)) {
                   $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
               }
               if(strlen($comments) < 2) {
                   $error_message .= 'The Comments you entered do not appear to be valid.<br />';
               }
               if(strlen($error_message) > 0) {
                   died($error_message);
               }
               $email_message = "Form details below.\n\n";
               function clean_string($string) {
                   $bad = array("content-type","bcc:","to:","cc:","href");
                   return str_replace($bad,"",$string);
               }
               $email_message .= "First Name: ".clean_string($first_name)."\n";
               $email_message .= "Last Name: ".clean_string($last_name)."\n";
               $email_message .= "Email: ".clean_string($email_from)."\n";
               $email_message .= "Telephone: ".clean_string($telephone)."\n";
               $email_message .= "Comments: ".clean_string($comments)."\n";
               // create email headers
               $headers = 'From: '.$email_from."\r\n".
                      'Reply-To: '.$email_from."\r\n" .
                      'X-Mailer: PHP/' . phpversion();
               @mail($email_to, $email_subject, $email_message, $headers);  
           ?>
           <!-- include your own success html here -->
           Thank you for contacting us. We will be in touch with you very soon.
           <?php } ?>
           <div id="text-container">
               <h1>Contact Us</h1>
               <form name="contactform" method="post" action="<?php $_PHP_SELF ?>">
                   <table width="450px">
                       <tr>
                           <td valign="top">
                               <label for="first_name">First Name *</label>
                           </td>
                           <td valign="top">
                               <input  type="text" name="first_name" maxlength="50" size="30">
                           </td>
                       </tr>
                       <tr>
                           <td valign="top"">
                               <label for="last_name">Last Name *</label>
                           </td>
                           <td valign="top">
                               <input  type="text" name="last_name" maxlength="50" size="30">
                           </td>
                       </tr>
                       <tr>
                           <td valign="top">
                               <label for="email">Email Address *</label>
                           </td>
                           <td valign="top">
                               <input  type="text" name="email" maxlength="80" size="30">
                           </td>
                      </tr>
                      <tr>
                          <td valign="top">
                              <label for="telephone">Telephone Number</label>
                          </td>
                          <td valign="top">
                              <input  type="text" name="telephone" maxlength="30" size="30">
                          </td>
                      </tr>
                      <tr>
                          <td valign="top">
                              <label for="comments">Comments *</label>
                          </td>
                          <td valign="top">
                              <textarea  name="comments" maxlength="1000" cols="25" rows="6"></textarea>
                          </td>
                      </tr>
                      <tr>
                          <td colspan="2" style="text-align:center">
                              <input type="submit" value="Submit">   <a href="http://www.freecontactform.com/email_form.php">Email Form</a>
                          </td>
                      </tr>
                  </table>
              </form>
          </div>
        </div>
    </div>
    <div id="footercontainer">
        <footer>Copyright  &copy;  2016   Dimaond Peaks Resort, &nbsp; All Rights Reserved.  <br/><a href="http://www.allwebpromotion.com/" target="_blank">Website Design</a> by: All Web Promotion</footer>
    </div>
</body>
</html>

2 个答案:

答案 0 :(得分:1)

您的died()函数的输出包含您在下面显示的相同联系表单,稍微重新构建您的代码。

摆脱death()函数中的表单

    function died($error) {

        echo "Bla bla bla";

        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 />";

    }

显示验证错误后显示“返回”按钮和表格(如果需要)

   <!-- include your own success html here -->
Thank you for contacting us. We will be in touch with you very soon.

<a href="<?php $_PHP_SELF ?>">Go Back</a>

<?php

}


// get rid or comment this condition if want show form with the errors messages  
if (!$error_message) {

?>


<div id="text-container">

<h1>Contact Us</h1>

表格......

<?php 
    }
?>

答案 1 :(得分:1)

您的nice和固定代码=)

<html>
<head>
<title> Contact Us </title>
<link rel="stylesheet" type="text/css" href="./main.css">
<link rel="icon" href="./ski_icon.png">
<script src="jquery-3.0.0.min.js" type="text/javascript"></script>
<script>
 $( document ).ready(function() {

    $("#container").css("height", 1135);


  });

  </script>

</head>
<body>


<style>

form
{
border: 1px solid transparent;
text-align: center;

}
</style>

<div id="content">
<div id="logo">
<img src="./logo.png"></img>
</div>
<div id="container">
<nav id="hor">
<div><a href="index.html"> Home </a> </div>
<div><a href="mountains.html"> The Mountains </a></div>
<div><a href="resorts.html"> The Resorts </a></div>
<div><a href="snowreports.html"> Snow Reports </a></div>
<div><a href="eventsandactivities.html"> Events &amp Activities </a></div>
<div><a href="plantrip.html"> Plan A Trip </a></div>
</nav>
<div id="middle">
<nav id="vert">
<a href="index.html"> Home </a> <br/>
<a href="mountains.html"> The Mountains </a><br/>
<a href="resorts.html"> The Resorts </a><br/>
<a href="snowreports.html"> Snow Reports </a><br/>
<a href="eventsandactivities.html"> Events &amp Activities </a><br/>
<a href="plantrip.html"> Plan A Trip </a>
</nav>

<style>

td
{

vertical-align: top;
}

</style>

<?php

if(isset($_POST['email'])) {



    // EDIT THE 2 LINES BELOW AS REQUIRED

    $email_to = "you@yourdomain.com";

    $email_subject = "Your email subject line";





    function died($error) {

        // your error code can go here



       echo "<div id='text-container'>" ;

echo "<h1>Contact Us</h1>";

echo "<form name='contactform' method='post' action='$_PHP_SELF'>";

echo"<table width='450px'>";

echo "<tr>";

echo" <td valign='top'>";

  echo "<label for='first_name'>First Name *</label>";

 echo "</td>";

echo " <td valign='top'>";

 echo" <input  type='text' name='first_name' maxlength='50' size='30' value='".(isset($_POST['first_name'])?$_POST['first_name']:'')."' >";

 echo "</td>";

echo "</tr>";

echo "<tr>";

 echo"<td valign='top'>";

  echo "<label for='last_name'>Last Name *</label>";

 echo "</td>";

 echo "<td valign='top'>";

  echo "<input  type='text' name='last_name' maxlength='50' size='30'  value='".(isset($_POST['last_name'])?$_POST['last_name']:'')."' >";

 echo "</td>";

echo "</tr>";

echo "<tr>";

echo "<td valign='top'>";

echo "<label for='email'>Email Address *</label>";

echo "</td>";

 echo "<td valign='top'>";

 echo "<input  type='text' name='email' maxlength='80' size='30'   value='".(isset($_POST['email'])?$_POST['email']:'')."'  >";

 echo "</td>";

echo "</tr>";

echo "<tr>";

 echo "<td valign='top'>";

 echo "<label for='telephone'>Telephone Number</label>";

echo "</td>";

echo "<td valign='top'>";

echo "<input  type='text' name='telephone' maxlength='30' size='30'     value='".(isset($_POST['telephone'])?$_POST['telephone']:'')."'   >";

echo "</td>";

echo "</tr>";

echo "<tr>";

echo "<td valign='top'>";

echo  "<label for='comments'>Comments *</label>";

echo "</td>";

echo "<td valign='top'>";

 echo "<textarea  name='comments' maxlength='1000' cols='25' rows='6'> ".(isset($_POST['comments'])?$_POST['comments']:'')."  </textarea>";

echo "</td>";

echo "</tr>";

echo "<tr>";

 echo "<td colspan='2' style='text-align:center'>";

  echo "<input type='submit' value='Submit'>   <a href='http://www.freecontactform.com/email_form.php'>Email Form</a>";

 echo "</td>";

echo "</tr>";

echo "</table>";

echo "</form>";

echo "Bla bla bla";

        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 />";


      echo "</div>";
echo "</div>";
echo "</div>";

echo "<div id='footercontainer'>";
echo "<footer>";
 echo "Copyright  &copy;  2016   Dimaond Peaks Resort, &nbsp; All Rights Reserved.  <br/>";
echo "<a href='http://www.allwebpromotion.com/' target='_blank'>Website Design</a> by: All Web Promotion";

echo "</footer>";
echo "</div>";
echo "</div>";

    }



    // validation expected data exists

    if(!isset($_POST['first_name']) ||

        !isset($_POST['last_name']) ||

        !isset($_POST['email']) ||

        !isset($_POST['telephone']) ||

        !isset($_POST['comments'])) {

        died('We are sorry, but there appears to be a problem with the form you submitted.');

    }



    $first_name = $_POST['first_name']; // required

    $last_name = $_POST['last_name']; // required

    $email_from = $_POST['email']; // required

    $telephone = $_POST['telephone']; // not required

    $comments = $_POST['comments']; // required



    $error_message = "";

    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

  if(!preg_match($email_exp,$email_from)) {

    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';

  }

    $string_exp = "/^[A-Za-z .'-]+$/";

  if(!preg_match($string_exp,$first_name)) {

    $error_message .= 'The First Name you entered does not appear to be valid.<br />';

  }

  if(!preg_match($string_exp,$last_name)) {

    $error_message .= 'The Last Name you entered does not appear to be valid.<br />';

  }

  if(strlen($comments) < 2) {

    $error_message .= 'The Comments you entered do not appear to be valid.<br />';

  }

  if(strlen($error_message) > 0) {

    died($error_message);

  }

    $email_message = "Form details below.\n\n";



    function clean_string($string) {

      $bad = array("content-type","bcc:","to:","cc:","href");

      return str_replace($bad,"",$string);

    }



    $email_message .= "First Name: ".clean_string($first_name)."\n";

    $email_message .= "Last Name: ".clean_string($last_name)."\n";

    $email_message .= "Email: ".clean_string($email_from)."\n";

    $email_message .= "Telephone: ".clean_string($telephone)."\n";

    $email_message .= "Comments: ".clean_string($comments)."\n";





// create email headers

$headers = 'From: '.$email_from."\r\n".

'Reply-To: '.$email_from."\r\n" .

'X-Mailer: PHP/' . phpversion();

@mail($email_to, $email_subject, $email_message, $headers);
exit;
?>



<!-- include your own success html here -->



Thank you for contacting us. We will be in touch with you very soon.



<?php

}

?>




<div id="text-container">

<h1>Contact Us</h1>

<form name="contactform" method="post" action="<?php $_PHP_SELF ?>">

<table width="450px">

<tr>

 <td valign="top">

  <label for="first_name">First Name *</label>

 </td>

 <td valign="top">

  <input  type="text" name="first_name" maxlength="50" size="30">

 </td>

</tr>

<tr>

 <td valign="top"">

  <label for="last_name">Last Name *</label>

 </td>

 <td valign="top">

  <input  type="text" name="last_name" maxlength="50" size="30">

 </td>

</tr>

<tr>

 <td valign="top">

  <label for="email">Email Address *</label>

 </td>

 <td valign="top">

  <input  type="text" name="email" maxlength="80" size="30">

 </td>

</tr>

<tr>

 <td valign="top">

  <label for="telephone">Telephone Number</label>

 </td>

 <td valign="top">

  <input  type="text" name="telephone" maxlength="30" size="30">

 </td>

</tr>

<tr>

 <td valign="top">

  <label for="comments">Comments *</label>

 </td>

 <td valign="top">

  <textarea  name="comments" maxlength="1000" cols="25" rows="6"></textarea>

 </td>

</tr>

<tr>

 <td colspan="2" style="text-align:center">

  <input type="submit" value="Submit">   <a href="http://www.freecontactform.com/email_form.php">Email Form</a>

 </td>

</tr>

</table>

</form>



  </div>
</div>
</div>

<div id="footercontainer">
<footer>
 Copyright  &copy;  2016   Dimaond Peaks Resort, &nbsp; All Rights Reserved.  <br/>
<a href="http://www.allwebpromotion.com/" target="_blank">Website Design</a> by: All Web Promotion

</footer>
</div>
</div>

</body>
</html>