dphfwzn8269 2019-04-12 09:22
浏览 127
已采纳

电子邮件表单输出错误(索引):305未捕获的TypeError:无法设置null的属性“innerHTML”

so i have a bootstrap 4 contact form set up which is linked to a validateForm() function which (of course) validates the form and sends it to the mail.php file.

However, upon entering the details needed and clicking the button to send the email, the form does nothing. I don't know whether the mail.php file is linked incorrectly or if its just my code.

this site is for an animation studio called Sutori Studios using the 000webhost hosting service as a live preview for them (which includes a system for email handling). I have checked to make sure its all linked together properly but i may have missed something.

<section id="contact" class="anchor">
            <!--Section heading-->
    <h2 class="h1-responsive font-weight-bold text-center my-4">Contact us</h2>
    <!--Section description-->
    <p class="text-center w-responsive mx-auto mb-5">Do you have any questions? Please do not hesitate to contact us directly. Our team will come back to you within
        a matter of hours to help you.</p>

    <div class="row">

        <!--Grid column-->
        <div class="col-md-9 mb-md-0 mb-5">
            <form id="contact-form" name="contact-form" action="mail.php" method="POST">

                <!--Grid row-->
                <div class="row">

                    <!--Grid column-->
                    <div class="col-md-6">
                        <div class="md-form mb-0">
                            <input type="text" id="name" name="name" class="form-control">
                            <label for="name" class="">Your name</label>
                        </div>
                    </div>
                    <!--Grid column-->

                    <!--Grid column-->
                    <div class="col-md-6">
                        <div class="md-form mb-0">
                            <input type="text" id="email" name="email" class="form-control">
                            <label for="email" class="">Your email</label>
                        </div>
                    </div>
                    <!--Grid column-->

                </div>
                <!--Grid row-->

                <!--Grid row-->
                <div class="row">
                    <div class="col-md-12">
                        <div class="md-form mb-0">
                            <input type="text" id="subject" name="subject" class="form-control">
                            <label for="subject" class="">Subject</label>
                        </div>
                    </div>
                </div>
                <!--Grid row-->

                <!--Grid row-->
                <div class="row">

                    <!--Grid column-->
                    <div class="col-md-12">

                        <div class="md-form">
                            <textarea type="text" id="message" name="message" rows="2" class="form-control md-textarea"></textarea>
                            <label for="message">Your message</label>
                        </div>

                    </div>
                </div>
                <!--Grid row-->

            </form>

            <div class="text-center text-md-left">
                <a class="btn btn-primary" onclick="validateForm()">Send</a>
            </div>
            <div class="status"></div>
        </div>
        <!--Grid column-->

        <!--Grid column-->
        <div class="col-md-3 text-center">
            <ul class="list-unstyled mb-0">
                <li><i class="fab fa-youtube fa-2x"></i>
                    <p>Sutori Studios</p>
                </li>

                <li><i class="fab fa-twitter mt-4 fa-2x"></i>
                    <p>@SutoriStudios</p>
                </li>

                <li><i class="fas fa-envelope mt-4 fa-2x"></i>
                    <p>SutoriStudios@gmail.com</p>
                </li>
            </ul>
        </div>
        <!--Grid column-->

    </div>
        </section>
    </div>
</main>
<footer class="page-footer font-small unique-color-dark">

  <!-- Copyright -->
  <div class="footer-copyright text-center py-3">© 2019 Copyright:
    <a href="index.html">Sutori Studios</a>
  </div>
  <!-- Copyright -->

</footer>
<!--Main layout-->
  <!-- /Start your project here-->

  <!-- SCRIPTS -->
  <!-- JQuery -->
  <script type="text/javascript" src="js/prerequisite/jquery-3.3.1.min.js"></script>
  <!-- Bootstrap tooltips -->
  <script type="text/javascript" src="js/prerequisite/popper.min.js"></script>
  <!-- Bootstrap core JavaScript -->
  <script type="text/javascript" src="js/prerequisite/bootstrap.min.js"></script>
  <!-- MDB core JavaScript -->
  <script type="text/javascript" src="js/prerequisite/mdb.js"></script>
  <script>
      function validateForm() {
            document.getElementById('status').innerHTML = "Sending...";
            formData = {
            'name'     : $('input[name=name]').val(),
            'email'    : $('input[name=email]').val(),
            'subject'  : $('input[name=subject]').val(),
            'message'  : $('textarea[name=message]').val()
            };


            $.ajax({
            url : "../public_html/mail.php",
            type: "POST",
            data : formData,
            success: function(data, textStatus, jqXHR)
            {

            $('#status').text(data.message);
            if (data.code) //If mail was sent successfully, reset the form.
            $('#contact-form').closest('form').find("input[type=text], textarea").val("");
            },
            error: function (jqXHR, textStatus, errorThrown)
            {
            $('#status').text(jqXHR);
            }
            });
      }
</script>
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$subject = $_POST['subject'];
header('Content-Type: application/json');
if ($name === '') {
    print json_encode(array('message' => 'Name cannot be empty', 'code' => 0));
    exit();
}
if ($email === '') {
    print json_encode(array('message' => 'Email cannot be empty', 'code' => 0));
    exit();
} else {
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        print json_encode(array('message' => 'Email format invalid.', 'code' => 0));
        exit();
    }
}
if ($subject === '') {
    print json_encode(array('message' => 'Subject cannot be empty', 'code' => 0));
    exit();
}
if ($message === '') {
    print json_encode(array('message' => 'Message cannot be empty', 'code' => 0));
    exit();
}

$content = "From: $name 
Email: $email 
Message: $message";
$recipient = "email.... (removed for privacy)";
$mailheader = "From: $email 
";
mail($recipient, $subject, $content, $mailheader) or die("Error!");
print json_encode(array('message' => 'Email successfully sent!', 'code' => 1));
exit();
?>

I expect to have an email delivered to the my mailbox but nothing gets sent through and i get an error in console stating:

(index):305 Uncaught TypeError: Cannot set property 'innerHTML' of null at validateForm ((index):305) at HTMLAnchorElement.onclick ((index):264)

  • 写回答

2条回答 默认 最新

  • drt3751 2019-04-12 10:01
    关注

    You have a div, having the class of status:

    <div class="status"></div>
    

    When you try to set its innerHTML, you are searching for an element having the id of status, which does not exist in your code:

    document.getElementById('status').innerHTML = "Sending...";
    

    and this throws the error that you have seen. Also, you try to set its innerText in a callback:

    $('#status').text(data.message);
    

    but again, you are searching for an element having an id of status. There are two ways to solve this, you can choose either of those, depending on your preference:

    Change the tag's structure to have an id of status:

    <div id="status" class="status"></div>
    

    and then your id selectors will find the element and your error will be fixed.

    Change the id selectors to class selectors

    var statusTags = document.getElementsByClassName("status");
    for (let statusItem of statusTags) statusItem.innerHTML = "Sending...";
    

    and

    $(".status").text(data.message);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog