weixin_33698823 2016-09-18 21:18 采纳率: 0%
浏览 19

通过ajax将var传递给php?

I am trying to pass values to php file. I am getting alert that The form was submitted but Nothing is displaying in php file. I checked the js variables and they are displaying correctly. This is my HTML form.

                         <form name="sentMessage"role="form" data-toggle="validator" id="ContactForm" onsubmit="myFunction()">
                                <div class="form-group has-feedback">
                                    <input type="text" pattern="^[_A-z ]{1,}$" class="form-control" placeholder="Your Name *" name="name" id="Username"
                                    data-error="Plese enter your name" required>
                                    <span class="glyphicon form-control-feedback" aria-hidden="true"></span>
                                    <span class="help-block with-errors"></span>

                                </div>

                                <div class="form-group has-feedback">
                                    <input type="email" class="form-control" placeholder="Your Email *" name="email" id="Useremail" required data-validation-required-message="Please enter your email address.">
                                    <span class="glyphicon form-control-feedback" aria-hidden="true"></span>
                                    <span class="help-block with-errors"></span>

                                </div>
                                <div class="form-group has-feedback">
                                    <input type="tel" pattern="^[_0-9 ]{1,}$" class="form-control" placeholder="Your Phone *" name="phone" id="Userphone" required data-validation-required-message="Please enter your phone number.">
                                    <span class="glyphicon form-control-feedback" aria-hidden="true"></span>
                                    <span class="help-block with-errors"></span>
                                </div>
                                <div class="form-group">
                                    <textarea class="form-control" placeholder="Your Message *" name="message" id="Usermesage" required data-validation-required-message="Please enter a message."></textarea>

                                    <p class="help-block text-danger"></p>
                                    <label class="light">
                                </div>


                                <button type="submit" class="btn btn-xl btn-success">
                                    Send Message
                                </button>
                        </div>
                        </form>

 <script>
            function myFunction() {
                var name = document.getElementById("Username").value;
                var email = document.getElementById("Useremail").value;
                var phone = document.getElementById("Userphone").value;
                var message = document.getElementById("Usermesage").value;

                $.ajax({
                  type: 'POST',
                  url: 'mailer.php',
                  data: {'name': name, 'email':email,'phone':phone,'message':message},
                  success: function(){
                  alert("The form was submitted");
                }
                });


            }
    </script>

PHP file mailer.php

<?php
if($_POST){
echo $name = $_POST['name'];
echo $email = $_POST['email'];
echo $phone = $_POST['phone'];
echo $message = $_POST['message'];


}

?>

Nothing is displaying in php file. Thank you

  • 写回答

1条回答 默认 最新

  • weixin_33711641 2016-09-18 21:39
    关注

    Two thoughts - are you submitting the form via normal form submit and then trying to submit the same form via the AJAX call. This would give no values to the AJAX call because they have already been submitted - you may need to prevent the normal form submission - there are plenty of answers in here related to that which will help you prevent the form submitting until the AJAX function.

    Second - I would go so far as to remove the submit button from inside the form, give it a type = "button" attribute and combine the onclick event handler and ajax call (note that I am using the serialize() method to gather all the relevant data from the form without specifically getting each inputs' value and am :-

    //html
        <button id="submitFormButton" type="button" class="btn btn-primary">Submit</button>
    
    //js
    $('#submitFormButton').click(function(){
        var formData = $('#contactForm').serialize();
        var URL = 'mailer.php';
          $.post(URL, formData,function( data ) {
           //function to perfrom on the returned data
          });
     });
    
    评论

报告相同问题?

悬赏问题

  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了
  • ¥15 电脑最近经常蓝屏,求大家看看哪的问题
  • ¥60 高价有偿求java辅导。工程量较大,价格你定,联系确定辅导后将采纳你的答案。希望能给出完整详细代码,并能解释回答我关于代码的疑问疑问,代码要求如下,联系我会发文档
  • ¥50 C++五子棋AI程序编写
  • ¥30 求安卓设备利用一个typeC接口,同时实现向pc一边投屏一边上传数据的解决方案。
  • ¥15 SQL Server analysis services 服务安装失败