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

Abhishek D

I'm a newbie to php and html, I've created a form which will accept the inputs from user and will send it to a specified Email id. Everything is working fine but on submitting the form, it goes to another new page. How this can be replaced with a dialog box as a confirmation message upon submitting the form?

HTML -

<form method="POST" name="contactform" action="contact-form-handler.php"> 
<p>
<label for='name'>Your Name:</label> <br>
<input type="text" name="name">
</p>
<p>
<label for='email'>Email Address:</label> <br>
<input type="text" name="email"> <br>
</p>
<p>
<label for='message'>Message:</label> <br>
<textarea name="message"></textarea>
</p>
<input type="submit" value="Submit"><br>
</form>

PHP -

<?php 
$errors = '';
$myemail = '[email protected]';//<-----Put Your email address here.
if(empty($_POST['name'])  || 
   empty($_POST['email']) || 
   empty($_POST['message']))
{
    $errors .= "\n Error: all fields are required";
}

$name = $_POST['name']; 
$email_address = $_POST['email']; 
$message = $_POST['message']; 

if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", 
$email_address))
{
    $errors .= "\n Error: Invalid email address";
}

if( empty($errors))
{
    $to = $myemail; 
    $email_subject = "Contact form submission: $name";
    $email_body = "You have received a new message. ".
    " Here are the details:\n Name : $name \n Email : $email_address \n Message : $message"; 

    $headers = "From: $myemail\n"; 
    $headers .= "Reply-To: $email_address";

    mail($to,$email_subject,$email_body,$headers);
    //redirect to the 'thank you' page
    echo "Your Message successfully sent, we will get back to you ASAP.";
} 
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html>
<head>
    <title>Contact form handler</title>
</head>

<body>
<!-- This page is displayed only if there is some error -->
<?php
echo nl2br($errors);
?>


</body>
</html>
Amit Gupta

You can submit the form in the same page and can simply show JavaScript alert message to the users.

Below code will help you resolving your issue.

<?php 
if(isset($_POST['btnSubmit'])){
$errors = '';
$myemail = '[email protected]';//<-----Put Your email address here.
if(empty($_POST['name'])  || 
   empty($_POST['email']) || 
   empty($_POST['message']))
{
    $errors .= "\n Error: all fields are required";
}

$name = $_POST['name']; 
$email_address = $_POST['email']; 
$message = $_POST['message']; 

if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", 
$email_address))
{
    $errors .= "\n Error: Invalid email address";
}

if( empty($errors))
{
    $to = $myemail; 
    $email_subject = "Contact form submission: $name";
    $email_body = "You have received a new message. ".
    " Here are the details:\n Name : $name \n Email : $email_address \n 
Message : $message"; 

    $headers = "From: $myemail\n"; 
    $headers .= "Reply-To: $email_address";

    mail($to,$email_subject,$email_body,$headers);
    //redirect to the 'thank you' page
    //echo "Your Message successfully sent, we will get back to you ASAP.";
} 
print("<script>alert('Your Message successfully sent, we will get back to 
you ASAP.');</script>");
}

?>

<form method="POST" name="contactform" action=""> 
<p>
<label for='name'>Your Name:</label> <br>
<input type="text" name="name">
</p>
<p>
<label for='email'>Email Address:</label> <br>
<input type="text" name="email"> <br>
</p>
<p>
<label for='message'>Message:</label> <br>
<textarea name="message"></textarea>
</p>
<input type="submit" value="Submit" name="btnSubmit"><br>
</form>

Hope it helps!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I show the output vertically after submitting the html form?

How to show dialog after success data

How to get a popup box after submitting a form?

How to add a success message after submitting Word form?

How to get the success message in the same page after submitting the form?

How to show dialog box after the precondition checks?

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

Form success msg disappearing after submitting

How to open a bootstrap modal box after submitting a form by barcode scanner

Show a modal dialog after the success in a "IF-STATEMENT"

How Show Success Message After form submit in wordpress?

Get the check box value after submitting a form

How can I show a dialog before submitting?

How to redirect to another HTML page after submitting a form?

How to open another submit form after submitting one on html or javascript?

Show bootstrap popover after submitting form that is in modal

How to show popup dialog box?

After submitting form its showing success message on different page

show input dialog box after error message dialog box

How to redirect after submitting form

show success message after form submit

How to update a datatable after submitting a dialog - JSF

Show a dialog box after a period of inactivity

Show properties dialog box after user creation

HTML5 show hidden form elements after submitting and showing the required tags

Show Confirm Alert Box before submitting the PHP Form

How can I reset a form field and show Success or Thanks you after form submission?

Modal box on submitting a form

Clear the input box after submitting the form in Angular 4