dongtuo0828 2013-12-05 16:09
浏览 16
已采纳

消息在PHP中传递确认

i am making a php website for my semester project ...and i have a contact us page in which user send me a message and the message is stored in database . but i want that when user type the message and click the send button then after checking form validation it send the user that "your message is delivered." and if there is something wrong in the validation then it tells user to type acc to requirements, here is my code.

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

and my form is :

                   <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条回答 默认 最新

  • dongxingdu9594 2013-12-05 16:25
    关注

    Assuming that your HTML code is in the same file as your PHP code you can add this to your HTML code:

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

    Just make sure that the PHP code and HTML code is both in the one .php file and the form action value is also the same .php file.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度