无法在Javascript中获得AJAX的响应

时间:2017-01-06 16:51:06

标签: javascript php ajax

我制作了一个简单的AJAX应用程序,它在某个时候运行良好,但我不知道我的应用程序运行不正常我做错了什么。我在这里提供所有信息:

  

这是HTML代码:

print("Each person will have %r sweets." %(s/p))
  

然后我按如下方式编写了Javascript代码:

<div class="col-lg-6 col-md-6 contact-wthree2">
        <h3 class="head2">Your Comments</h3>
        <form onsubmit="submit_form();">
            <div class="row">
                <div class="form-group col-lg-6 col-md-6 col-sm-6 slideanim">
                    <input type="text" class="form-control first-name" id="firstname" placeholder="First Name"
                           required/>
                </div>
                <div class="form-group col-lg-6 col-md-6 col-sm-6 slideanim">
                    <input type="text" class="form-control last-name" id="lastname" placeholder="Last Name"
                           required/>
                </div>
                <div class="form-group col-lg-6 col-md-6 col-sm-6 slideanim">
                    <input type="email" class="form-control mail" id="mail" placeholder="Your Email" required/>
                </div>
                <div class="form-group col-lg-6 col-md-6 col-sm-6 slideanim">
                    <input type="tel" class="form-control pno" id="phone" placeholder="Your Phone Number"
                           required/>
                </div>
                <div class="clearfix"></div>
                <div class="form-group col-lg-12 slideanim">
                    <textarea class="form-control" rows="6" id="message" placeholder="Your Message" required></textarea>
                </div>
                <div class="form-group col-lg-12 slideanim">
                    <button type="submit" class="btn btn-lg btn-outline">Send Message</button>
                </div>
            </div>
        </form>
    </div>
</div>

<button class="btn btn-primary" id="clickme" data-toggle="modal" data- target="#dialog" style="display:none">Open Dialog</button>

<div class="modal fade" id="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header" id="response">

            </div>

        </div>

    </div>

</div>  
  

这是我的PHP文件(process_comments.php):

<script>
    var xmlhttp = new XMLHttpRequest();


        var firstname =  document.getElementById('firstname').value,
        lastname = document.getElementById('lastname').value,
        email = document.getElementById('mail').value,
        contact = document.getElementById('phone').value,
        message = document.getElementById('message').value;

    function submit_form()
    {
        xmlhttp.onreadystatechange = function()
        {

            if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200))
            {

                document.getElementById('response').innerHTML = xmlhttp.responseText;
                document.getElementById('clickme').click();

                firstnameinp.value="";
                 lastnameinp.value="";
                 emailinp.value="";
                 contactinp.value="";
                 messageinp.value="";


            }
        }
        xmlhttp.open('POST','process_comments.php?firstname=' + firstname+'&lastname='+lastname
            +'&email='+email+'&contact='+contact+'&message='+message+'',true);
        xmlhttp.send();

    }



</script>  

这已经工作了一段时间,但现在当我运行它时,它没有给出xmlhttp.responseText。
有人请帮忙。

0 个答案:

没有答案