简体   繁体   English

如何提交表单而不刷新当前页面?

[英]How do I submit a form without refreshing the current page?

I have a fairly basic email form 我有一个基本的电子邮件表格

<form name="contactform" method="post" action="send_email.php" id="email_form">
  <div class="ContactHeaders">Name</div>
  <input type="text" name="Name" class="ContactBoxes" id="name"/><br/><br/>
  <div class="ContactHeaders">Email</div>
  <input type="email" name="Email" class="ContactBoxes"/><br/><br/>
  <div class="ContactHeaders">Message</div>
  <div style="width:100%">
    <textarea name="Message" maxlength="1000"></textarea><br/>
  </div>
  <div style="width: 100%">
    <input type="submit" class="Submitbtn" value="Submit">
  </div>
</form>

Here's 'send_email.php' 这是“ send_email.php”

<?php
ob_start();
include 'navbar.php';
ob_end_clean();

if(isset($_POST['Email'])) {

  //declare variables
  $Name = $_POST['Name'];
  $Email = $_POST['Email'];
  $Message = $_POST['Message'];
  $complete = 0;
  $email_to = "someone@example.com";
  $email_subject = "Website Contact";

  //check all forms are filled in correctly

  $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  if(preg_match($email_exp,$Email)) {
     $complete++;
     }
  $string_exp = "/^[A-Za-z .'-]+$/";
  if(preg_match($string_exp,$Name)) {
     $complete++;
     }
  if(strlen($Message) > 20) {
     $complete++; 
     } 
  //send email if $complete = 4
  if ($complete == 3){
      if (get_magic_quotes_gpc()) {
          $Message = stripslashes($Message);
      }
  $Message =  $Message . "\n\n" . "From " . $Name;
  $headers = 'From: '.$Email."\r\n".
 'Reply-To: '.$Email."\r\n" .
 'X-Mailer: PHP/' . phpversion();
 @mail($email_to, $email_subject, $Message, $headers);
 echo '<script>
     alert("Thank you for contacting me. I will be in touch shortly");
     top.location="index.php";
     </script>';
 }
 else {
     echo '<script>
             alert("The form you submitted is invalid. Please ensure the following: \n  You have provided a valid email address. \n  Your phone number is entered correctly. \n  The message box contains at least 20 characters.")
             history.go(-1)
          </script>';
    }
}

?>

So my problem is that when the form submits to the php file, the browser loads the php file, runs the code and returns the result (It's not really a problem but it's something I don't want) It is then up to java to display the alert and go back one (in my code anyway). 所以我的问题是,当表单提交到php文件时,浏览器会加载php文件,运行代码并返回结果(这不是真正的问题,但这是我不想要的东西),然后由java决定显示警报并返回一个警报(无论如何以我的代码)。 Is there a way to make it run the code in the background so the form runs the php file without having to go to it (if that makes sense) and return the result in the same window. 有没有一种方法可以使它在后台运行代码,以便表单无需运行即可运行php文件(如果可以的话)并在同一窗口中返回结果。 I've obviously looked around and found loads of things about AJAX but I didn't really understand it and couldn't get it to work for me. 我显然环顾四周,发现了许多有关AJAX的内容,但我并不真正了解它,也无法让它为我工作。

The reason for doing this is a little complicated but would make things much easier as far as user-friendliness goes for my site, as well as looking cleaner (going to a blank page and displaying an alert doesn't look very good). 这样做的原因有些复杂,但就我的网站而言,就用户友好性而言,它使事情变得容易得多,而且看起来更加简洁(进入空白页并显示警报看起来不太好)。

Thanks in advance for any help you can offer me:) 预先感谢您可以为我提供的任何帮助:)

$('.Submitbtn').click(function(e) {
    e.preventDefault();
    // do some form validation here
    if form is valid { // from validation above
        var formData = $('#email_form').serialize();
        $.post('send_email.php',{data:formData});
    } else {
        alert('Form no good - fix it!');
        return false;
    }
});

Here's something you can start with. 您可以从这里开始。 This is very basic but supplies the necessary foundation for beginning your AJAX adventure. 这是非常基础的,但是为开始AJAX冒险提供了必要的基础。

50 Excellent AJAX Tutorials 50个优秀的AJAX教程

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

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