douqin6785 2018-08-23 13:32
浏览 79
已采纳

重新加载页面(发送表单)

I have a problem with reloading the page. After clicking submit form the page reloads. I click send the form and takes me to http: // localhost: 3000 / mail.php. And I would like the site not to be reloaded. (I use the validation form jquery plugin).This is my code: jquery

  (function(){
    $("#contactForm").on('submit', function(e) {
          e.preventDefault();
          var data = {
            name = $('#field-name').val(),
            phone = $('#field-phone').val(),
            email = $('#field-email').val(),
            message = $('#field-message').val()
        };
        $.ajax({
            type: "POST",
            url: "mail.php",
            data: data,
            success: function(){
                console.log("jej");
            }
        });
        return false;
      });
  });

and php code

<?php
    $to = 'name@gmail.com';
    $name = $_POST['name'];
    $phone = $_POST['phone'];
    $email= $_POST['email'];
    $text = $_POST['message'];
    $subject = 'Nowy e-mail od ' . $name . ' (' . $email . ')';
    $message =  $name  . $phone .  $email . $text;
    $headers = 'From: ' . $name . "
" .
    if(mail($to, $subject, $message, $headers)) {
      print "<p class='success'>Mail Sent.</p>";
    } else {
        print "<p class='Error'>Problem in Sending Mail.</p>";
    }

?>

  • 写回答

1条回答 默认 最新

  • dongzhang0243 2018-08-23 13:48
    关注

    change part of your code

    $("#contactForm").on('submit', function(e) 
    

    to this...

    $("#contactFormButton").on('click', function(e) 
    

    and change your submit button to this...

    <button id="contactFormButton" type="button" class="form-control">Submit</button>
    

    and here your php code...

    if(mail($to, $subject, $message, $headers)) {
          echo "Mail Sent!";
        } else {
            echo "Error!!";
        }
    

    and here you can catch result of your php code...

            success: function(data){
                console.log(data);
            }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条