如何使用Javascript定位我的PHP联系人表单警报消息

How to position my PHP contact form alert message using Javascript?

本文关键字:表单 消息 联系人 PHP 何使用 Javascript 定位 我的      更新时间:2023-09-26

警告信息定位问题(您需要填写所有必填字段!!)不能通过CSS来实现,因为元素没有任何地方可以获取它们的位置。

任何想法?

    <!DOCTYPE html>
<html lang="en-US">
    <head>
    <meta charset="utf8" />
        <link rel="stylesheet" type="text/css" media="all" href="css/mainstyle.css"/>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
        <script src="js/everypage.js"></script>
        <meta name="viewport" content="initial-scale=1" />
        <link rel="shortcut icon" href="IMG/tabicon/favicon.ico" >
        <title> A band </title>


<?php
        $name = $_POST['name'];
        $email = $_POST['email'];
        $message = $_POST['message'];
        $from = 'From: Website'; 
        $to = 'info@example.com'; 
        $subject = 'Hello';
        $human = $_POST['human'];
        $body = "From: $name'n E-Mail: $email'n Message:'n $message";
        if ($_POST['submit']) {
        if ($name != '' && $email != '') {
            if ($human == '4') {                 
                if (mail ($to, $subject, $body, $from)) { 
                echo '<p class="alert">Your message has been sent!</p>';
            } else { 
                echo '<p class="alert">Something went wrong, go back and try again!</p>'; 
            } 
        } else if ($_POST['submit'] && $human != '4') {
            echo '<p class="alert">You answered the anti-spam question incorrectly!</p>';
        }
        } else {
            echo '<p class="alert">You need to fill in all required fields!!</p>';
        }
    }
    ?>
    </head>
    <body>
    <div id="background">
        <div id="wrapper">

            <div class="navigation">
                <ul id="mainmenu">
                    <li><a href="index.html">Home</a></li>  
                    <li><a href="band.html">Band</a></li>
                    <li><a href="news.html">News</a></li>
                    <li><a href="shows.html">Shows</a></li>
                    <li><a href="music.html">Music</a></li>
                    <li><a href="gallery.html">Gallery</a></li>
                    <li><a href="media.html">Media</a></li>
                    <li><a href="store.html">Store</a></li>                         
                </ul>
            </div>
            <div class="article">
                <div id="logo"></div>
                <div class="contacts">
                        <h1> Contact, booking:</h1>
                        <p> info@example.com </p>
                        <p> Tel: +372 58 131 054</p>
                </div>    

              <form method="post" action="contact.php">
                    <label>Name</label>
                    <input name="name" placeholder="Type Here">
                    <label>Email</label>
                    <input name="email" type="email" placeholder="Type Here">
                    <label>Message</label>
                    <textarea name="message" placeholder="Type Here"></textarea>
                    <input id="submit" name="submit" type="submit" value="Submit">
                    <label>*What is 2+2? (Anti-spam)</label>
                    <input name="human" placeholder="Type Here">   
            </form>
            </div>                     
            </div>
                <div class="footer contactfooter">
                    <p class="bandmembers">content</p>
                    <p class="signature">content</p>
                        <ul id="footermenu">
                            <li><a href="terms.html">Terms of use</a></li>
                            <li><a href="privacy.html">Privacy Policy</a></li>
                            <li><a href="contact.php">Contact</a></li>
                       </ul>
                </div>
          </div> 
    </div>
</body>
</html>

您可以通过wrap将相关输入(s)和使用position:relative(包装器元素)来定位消息元素来定位警报<p>标记。

在JavaScript验证中:

  1. 确定哪个表单元素应该作为消息放置的提示。
  2. 包装表单元素
  3. 将警告附加到包装器。

validationWrapper类应该避免为目标元素添加填充/边距。它应该是放置alert类元素的起始点。

下面的代码使用了jQuery JavaScript库:
$(formElem).wrap("<div class='"validationWrapper'"></div>");
$(".validationWrapper").append("<p class='"alert'">"+message+"</p>");
$(".validationWrapper .alert").css({ position: "relative", left: 0, top: "-5em"});

同样,你的表单很容易受到垃圾邮件发送者的攻击。To头应该设置在服务器端。没关系,没有注意到PHP的标签

对于您的问题,我建议您使用jquery进行验证和显示消息。这不仅可以帮助您将消息定位到您想要的位置,还可以为您的消息提供样式选择。使用此链接可以更好地理解