简体   繁体   English

PHP表单提交到同一页面后显示消息

[英]Display message after php form submit INSIDE same page

on my INDEX.HTML page I have a subscription form that writes data to a DB with php. 在我的INDEX.HTML页面上,我有一个订阅表单,该表单使用php将数据写入数据库。 Everything works honky-dori. 一切正常。

When I submit the form the site goes to the PHP file I use to submit the emails to me and the visitor. 当我提交表单时,站点将转到PHP文件,我用它向我和访客提交电子邮件。 I then can echo a thank you message. 然后,我可以回声谢谢消息。 However...I dont want the message to appear inside this php page....I want the form to disappear and the message appear on my index page inside aa tag...eg a DIV. 但是...我不希望该消息出现在此php页面中。...我希望该表单消失,并且该消息出现在我的索引页中的一个标签内,例如DIV。

How do I do this? 我该怎么做呢? If I need to use AJAX or JQUERY...could you please point me to the right place? 如果我需要使用AJAX或JQUERY ...您能指出我正确的地方吗?

Here are some of the code: 以下是一些代码:

 <div class="container-fluid">
 <form action="thankyou.php" method="post">
     <div class="form-group">
       <label for="firstname">First Name:</label>
       <input type="input" class="form-control" id="firstname"  name="firstname">      
       <label for="lastname">Last Name:</label>
       <input type="input" class="form-control" id="lastname" placeholder="" name="lastname">
       <label for="email">Email:</label>
       <input type="email" class="form-control" id="email" placeholder="" name="email">      
       </div>
       <button type="submit" class="btn btn-warning">Submit</button>
   </form>
   </div>

thankyou.php Thankyou.php

<?php

require 'connection.php';
$conn         = Connect();
$firstname    = $conn->real_escape_string($_POST['firstname']);
$lastname     = $conn->real_escape_string($_POST['lastname']);
$email        = $conn->real_escape_string($_POST['email']);
$myemail      = "myemailaddress";    
$query        = "INSERT into subscription (firstname,lastname,email) VALUES('" . $firstname . "','" . $lastname . "','" . $email . "')";
$success      = $conn->query($query);
$subject1     = "New eBook Subscriber";


if (!$success) {
    die("Couldn't enter data: ".$conn->error);
} else {

  $headers  = "MIME-Version: 1.0" . "\r\n";
  $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
  $headers .= 'From: ' .$myemail. "\r\n";


 $message = "some message";

  $messageb = "some message";

  mail($email, $subject, $messageb, $headers);    
  mail($myemail, $subject1, $message, $headers); 

?><META HTTP-EQUIV="Refresh" CONTENT="1;URL=index.html">

<?php 
$conn->close();
}
?>

Try this solution 试试这个解决方案

<div class="container-fluid">
 <form action="thankyou.php" method="post" id="form">
     <div class="form-group">
       <label for="firstname">First Name:</label>
       <input type="input" class="form-control" id="firstname"  name="firstname">      
       <label for="lastname">Last Name:</label>
       <input type="input" class="form-control" id="lastname" placeholder="" name="lastname">
       <label for="email">Email:</label>
       <input type="email" class="form-control" id="email" placeholder="" name="email">      
       </div>
       <button type="submit" class="btn btn-warning">Submit</button>
   </form>
   </div>

<script>


$('#form').on('submit', function(event) {
 event.preventDefault(); //stops form on submit
  var formData = {};
  $.each($("#form").serializeArray(), function (i, field) {
    formData[field.name] = field.value;
  });
  $.ajax({
  url: 'thankyou.php',
  data: formData,
  method:'POST',
  success: function(response) { 
   $(this).hide(); //sets css display:none to form
   var message = "Thank you!";  
   $('.container-fluid').html(message);
   }
});    

});
</script>

Set the redirect after the successfully email the data and set a session variable and print this session variable in the html page. 在成功通过电子邮件发送数据后设置重定向,并设置会话变量并在html页面中打印此会话变量。 this may solve your problem however you can also use ajax to send the data and solve this problem. 这可能会解决您的问题,但是您也可以使用ajax发送数据并解决此问题。

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

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