Display message after php form submit INSIDE same page

user5900363

on my INDEX.HTML page I have a subscription form that writes data to a DB with 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. 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 a a tag...e.g a DIV.

How do I do this? If I need to use AJAX or JQUERY...could you please point me to the right place?

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

<?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();
}
?>
Sathishkumar Rakkiasamy

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>

Este artículo se recopila de Internet, indique la fuente cuando se vuelva a imprimir.

En caso de infracción, por favor [email protected] Eliminar

Editado en
0

Déjame decir algunas palabras

0Comentarios
Iniciar sesiónRevisión de participación posterior

Artículos relacionados

Show Success Message After form submit in wordpress dashboard

How to display form data in popup after clicking on submit button

Displaying a specific section of a page after refresh or form submit

Collect values of multiple checkboxes in PHP (2 submit in same form ?)

Reload page after reactive form submission inside a dialog

$_POST empty after form submit

How to redirect to another page after click on submit button in form with required inputs

Display character " inside JsonResponse message

show the sucess popup after form submit form?

form.on('submit') does not trigger after form.submit() but before

replace the form with a message -not with PHP

Чтение вложенного JSON в ответе PHP из form.submit, а НЕ из магазина

Server side error message display from process page to index page in php

Align form field and submit button on same line

Display Checkbox values after submit button

jQuery callback after Gravity Form submit

Onsubmit event after javascript form submit

how to clear form after submit in next js?

How to call component, after submit form success?

How to reset form after submit using angular?

Vue Prevent form submit when pressing enter inside form

Submit a PHP Form with any of keyboard shortcut key

How to display message from BindingResult.reject("message") in thymeleaf page

Display selected form items in another page with Flask

React.js - on form submit, post data to backend and redirect page

Вызов $ ("# form"). Submit (... ничего не делает

Display a specific div depending on value generated after submit

Empty response after submit a form with (requests) python package

jQuery click event does not work - after form submit