简体   繁体   English

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

[英]Reloading the page (send form)

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.我单击发送表单并将我带到 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 (我使用验证表单 jquery 插件)。这是我的代码: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代码

<?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 . "\r\n" .
    if(mail($to, $subject, $message, $headers)) {
      print "<p class='success'>Mail Sent.</p>";
    } else {
        print "<p class='Error'>Problem in Sending Mail.</p>";
    }

?> ?>

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...在这里你的 php 代码...

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

and here you can catch result of your php code...在这里你可以捕捉你的 php 代码的结果......

        success: function(data){
            console.log(data);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM