How to show success message below html form after form submit which is being handled by different php file

user1896653

There is a form in my index.php file. This form is handling from a different php file named send-mail.php. I want to show a message inside alert div in index.php file. Can this be done by php or javascript will be needed too?

index.php:

<section id="contact">
<form action="send-mail.php" id="form" method="post" name="form">
   <input id="name" name="name" placeholder="your name" type="text" required>
   <input id="email" name="email" placeholder="your e-mail" type="email" required>
   <textarea cols="50" id="message" name="message" placeholder="your enquiry" rows="4" required></textarea>
   <input type="submit" name="submit" id="submit" value="Send Message">
</form>
<div class="alert alert-dismissible fade in hide" role=alert>
   <button type=button class=close data-dismiss=alert aria-label=Close><span aria-hidden=true>&times;</span></button>
</div>
</section>

send-mail.php:

<?php
if(isset($_POST['submit'])){
    // Get the submitted form data
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];

    // Recipient email
    $toEmail = '[email protected]';

    $emailSubject = 'Contact Request Submitted by '.$name;
    $htmlContent = '<h2>Contact Request Submitted</h2>
        <h4>Name</h4><p>'.$name.'</p>
        <h4>Email</h4><p>'.$email.'</p>
        <h4>Message</h4><p>'.$message.'</p>';

    // Set content-type header for sending HTML email
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

    // Additional headers
    $headers .= 'From: '.$name.'<'.$email.'>'. "\r\n";

    // Send email
    if(mail($toEmail,$emailSubject,$htmlContent,$headers)){
        $statusMsg = 'Your contact request has been submitted successfully !';
        $msgClass = 'alert-success';
        header('location: index.php#contact');
    }else{
        $statusMsg = 'Your contact request submission failed, please try again.';
        $msgClass = 'alert-danger';
        header('location: index.php#contact');
    }

}
?>
Adam B

Change your redirects to:

header('location: index.php?result='.$msgClass.'#contact');

Then adding the following to your index.php file:

if ($_GET['result']=="alert-success") {
    // display success message here
} elseif ($_GET['result']=="alert-danger") {
    // display error message here
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Replacing HTML form with success message after submit, form sends mail using separate php file

show success message after form submit

How Show Success Message After form submit in wordpress?

Show Success Message After form submit in wordpress dashboard

PHP submit form and stay on form with success message

How to add success notification after form submit

How to call component, after submit form success?

How to show a success dialog box after submitting the form in html?

HTML, PHP: Variables doesn't show up after form submit

JavaScript success message after form submit/page re-load

display two id# in same success message after form submit

How do I get errors which are handled serverside in a ajax error message after submitting a form?

How to hide the html form and then show the output after clickin on submit button?

Submit a HTML form without redirection and showing a success message

show a div after a html form submit

How to display success message in a form submit after sending a mail successfully using malsup form jquery without page refresh

How to show an error message in each of different field for PHP Contact form?

How to show a "please wait" message to a user after form submits in HTML

After submitting form its showing success message on different page

how to show the output after the form submit in JavaScript

How to submit a form if reCaptcha success

Replacing html form with success message

add 'success' message to html form

Show message in div in submit form

Hide form after submit with php / html

PHP form escapes html field after submit

AngularJS form validation: show error message for checkbox after click on submit

How to redirect back to html after JSP Form submission (with success/error message)

Form: Show entry after a click below a form

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  6. 6

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  7. 7

    Do Idle Snowflake Connections Use Cloud Services Credits?

  8. 8

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  9. 9

    Binding element 'string' implicitly has an 'any' type

  10. 10

    BigQuery - concatenate ignoring NULL

  11. 11

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  12. 12

    In Skype, how to block "User requests your details"?

  13. 13

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  14. 14

    Pandas - check if dataframe has negative value in any column

  15. 15

    flutter: dropdown item programmatically unselect problem

  16. 16

    Generate random UUIDv4 with Elm

  17. 17

    Is it possible to Redo commits removed by GitHub Desktop's Undo on a Mac?

  18. 18

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  19. 19

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  20. 20

    EXCEL: Find sum of values in one column with criteria from other column

  21. 21

    How to use merge windows unallocated space into Ubuntu using GParted?

HotTag

Archive