douzhang3356 2015-12-12 23:39
浏览 13

模态联系表。 每当刷新页面时提交电子邮件。 无法显示错误消息。

Hullo. I am trying to make a modal appear on my website that will let people who visit send messages to an email i have designated. I am completely new to coding and have spent many hours trawling these forums for help. I followed this tutorial: https://bootstrapbay.com/blog/working-bootstrap-contact-form/.

This is my html:

<div class ="modal fade" id="contact" role="dialog"> 
        <div class="modal-dialog">
            <div class="modal-content">
                <form class="form-horizontal" role="form" method="post" action="contact.php"> 
                    <div class="modal-header">
                        <h4> Contact </h4>
                    </div>
                    <div class="modal-body">
                            <div class="form-group">
                                    <label for="contact-name" class="col-sm-2 control-label">Name</label>
                                    <div class="col-sm-10">
                                        <input name="nameinput" type="text" class="form-control" id="contact-name" placeholder="First & Last Name">
                                        <?php echo "<p class='text-danger'> $errName </p>";?> 
                                    </div>
                            </div>
                    </div>
                    <div class="modal-body">
                            <div class="form-group">
                                    <label for="contact-email" class="col-sm-2 control-label">Email</label>
                                    <div class="col-sm-10">
                                        <input name="emailinput" type="email" class="form-control" id="contact-email" placeholder="example@gmail.com">
                                        <?php echo "<p class='text-danger'>$errEmail</p>";?> 
                                    </div>
                            </div>
                    </div>
                    <div class="modal-body">
                            <div class="form-group">
                                    <label for="contact-message" class="col-sm-2 control-label">Message</label>
                                    <div class="col-sm-10">
                                        <textarea name = "messageinput" class="form-control" id="contact-message" rows="4"></textarea>
                                        <?php echo "<p class='text-danger'>$errMsg</p>";?>  
                                    </div>
                            </div>
                    </div>    
                    <div class="modal-body">
                            <div class="form-group">
                                <label for="human" class="col-sm-2 control-label"> 2 + 3 ? </label>
                                <div class = "col-sm-10">
                                    <input type = "text" class="form-control" id="human" name="human" placeholder="Type your answer here">
                                     <?php echo "<p class='text-danger'>$errHuman</p>";?>
                                </div>
                            </div>
                    </div>
                    <div class="modal-body">
                            <div class="form-group">
                                <div class="col-sm-10 col-sm-offset-2">
                                    <!-- will be used to display an alert to the user -->
                                </div>
                            </div>
                    </div>
                    <div class="modal-footer">
                        <div class = "form-group">
                            <div class="col-sm-10 col-sm-offset-2">
                                <a class="btn btn-default" data-dismiss="modal">Close</a>
                                <input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary">
                        </div>

This is my php:

<?php    
        $errName='' ;
        $errEmail='';
        $errMsg='';
        $result='';
        $errHuman='';

        //have to define variables outside of if statements, even if you just set them to null or ''//

        
        if(isset($_POST["submit"])) {
            $name = $_POST['nameinput'];
            $email = $_POST['emailinput'];
            $message = $_POST['messageinput'];
            //$human = intval($_POST['human']);
            $from = 'From: $name';
            $to = 'danroebuck1994test@gmail.com';
            $subject = 'Message from Contact Form';
                    
            $body ="From: $name
 E-Mail: $email
 Message:
 $message";
        
            

    
        
            // Check if name has been entered
            if (!$_POST['nameinput']) {
                $errName = 'Please enter your name'; 
            }

            // Check if email has been entered and is valid
            if (!$_POST['emailinput'] || !filter_var($_POST['emailinput'], FILTER_VALIDATE_EMAIL)) {
                $errEmail = 'Please enter a valid email address';
            }

            //Check if message has been entered
            if (!$_POST['messageinput']) {
                $errMsg = 'Please enter your message';
            } 

            //If there are no errors, send the email
            if (!$errName && !$errEmail && !$errMsg) {
                if (mail ($to, $subject, $body, $from)) {
                    echo "<script type='text/javascript'>alert('Email sent successfully')</script>";
                } else {
                    echo "<script type='text/javascript'>alert ('An error occurred, the email has not been sent. Please try again later.')</script>";
                }
            //}
        }
?>

The first trouble I am having is that when I open up the modal by pressing the contact button, the red text is not displaying below the fields to ask for the required inputs. The second issue is that whenever I refresh the page, the previous email I set up in the form is sent lol, with the alert popping up saying 'your email has been sent successfully'. This seemed very weird to me as I thought using the if(isset($_POST["submit"])) would mean it would only be sent if the submit button was clicked.

I ask for any assistance possible. I did search these forums for a solution and I found this: PHP email form sends email everytime page is refreshed. However I did not find it helpful to me. As this was 6 hours ago and I have now made no progress I figured it would be reasonable to ask. Thankyou friends.

</div>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 安卓adb backup备份应用数据失败
    • ¥15 eclipse运行项目时遇到的问题
    • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
    • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
    • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
    • ¥50 成都蓉城足球俱乐部小程序抢票
    • ¥15 yolov7训练自己的数据集
    • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
    • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
    • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)