消息在PHP中传递确认

时间:2013-12-05 16:09:20

标签: php database

我正在为我的学期项目制作一个php网站...我有一个联系我们页面,用户向我发送消息,消息存储在数据库中。但我希望当用户键入消息并单击发送按钮然后在检查表单验证后,它会向用户发送“您的消息已发送”。如果验证中有错误,它会告诉用户输入acc to requirements, 这是我的代码。

    <?php
    // define variables and set to empty values
   $yournameErr = $emailErr = $messageErr = "";
     $yourname = $email = $message = "";

     if ($_SERVER["REQUEST_METHOD"] == "POST")
      {

         if (empty($_POST["yourname"]))
           {$yournameErr = "Name is required"; }
         else
           {$yourname = test_input($_POST["yourname"]);}

         if (empty($_POST["email"]))
           {$emailErr = "Email is required";}
              else
                {$email = test_input($_POST["email"]);
                  // check if e-mail address syntax is valid
                  if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
                     {
             $emailErr = "Invalid email format"; 
            }
           }

           if (empty($_POST["message"]))
               {$messageErr = "Message is required"; }
             else
              {$message = test_input($_POST["message"]);}
                   }

                function test_input($data)
              {
             $data = trim($data);
                 $data = stripslashes($data);
               $data = htmlspecialchars($data);
             return $data;
                   }

我的表格是:

                   <form action="contact.php" name="Form1" id="Form1" method="post">
                    <div>
                        <label>Your Name:</label>
                        <br />
                        <input type="text" name="yourname"          
     id="yourname" placeholder="Full Name" style="border:1;  border-color:#000000; " />
                        <span class="error">* <?php echo  
        $yournameErr;?></span>
                    </div>

                    <br />
                    <br />
                    <div>
                        <label> Email :</label> <br />
                        <input name="email" type="text" 
  id="email" size="20" placeholder="Email" style="border:1;  border-color:#000000; " />
                        <span class="error">* <?php echo 
    $emailErr;?></span>
                    </div>
                    <br />
                    <br />
                    <div>
                        <label> Subject : </label><br />
                        <input name="subject" type="text" 
        id="subject" size="20" placeholder="Subject" style="border:1;  border-
        color:#000000; "  />
                    </div>
                    <br />
                    <br />
                    <div>
                        <label> Message :<br /> </label>
                        <textarea rows="5" cols="40"  
         name="message" type="text" id="message" placeholder="The message you want to 
           send to us." style="border:1;  border-color:#000000 " >
                        </textarea>
                        <span class="error">* <?php echo 
         $messageErr;?></span>
                    </div> 
                    <br />
                    <br />
                    <div>
                        <input type="submit" name="button" 
          id="button" style="border:1; border-color:#999999; " value="SEND"/>
                    </div>


          </form>

1 个答案:

答案 0 :(得分:0)

假设您的HTML代码与PHP代码位于同一个文件中,您可以将其添加到HTML代码中:

<?php if(isset($messageErr) && $messageErr){ ?>
<div class="error"><?php echo $messageErr; ?></div>
<?php } ?>
<?php if($_SERVER["REQUEST_METHOD"] == "POST" && !$messageErr){ ?>
<div class="error">Message Delivered</div>
<?php } ?>

只需确保PHP代码和HTML代码都在一个.php文件中,并且表单操作值也是相同的.php文件。