John Davidson

javascript - php sucess message didnot work after send mail

0 comments
Message:


in contact.php i write a form to get the details, and will submit to contactprocess.php . after mail that i want to show a successs message and redirect to contact.php. in this code mail was successfully sented but alert and redirect not work.


contact.php


<form class="form-contact contact_form" action="contact_process.php" method="post" id="contactForm" novalidate="novalidate">
<div class="row">
<div class="col-12">
<div class="form-group">
<input class="form-control" name="subject" id="subject" type="text" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Enter Subject'" placeholder="Subject">
</div>
</div>

<div class="col-12">
<div class="form-group">
<textarea class="form-control w-100" name="message" id="message" cols="30" rows="9" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Enter Message'" placeholder="Message"></textarea>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<input class="form-control valid" name="name" id="name" type="text" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Enter your name'" placeholder="name">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<input class="form-control valid" name="email" id="email" type="email" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Enter email address'" placeholder="Email">
</div>
</div>

</div>
<div class="form-group mt-3">
<button type="submit" class="button button-contactForm boxed-btn">Send</button>
</div>
</form>

contact_process.php


<?php
$to = "[email protected]";
$from = $_REQUEST['email'];
$name = $_REQUEST['name'];
$csubject = $_REQUEST['subject'];
$cmessage = $_REQUEST['message'];

$headers = "From: $from";
$headers = "From: " . $from . "\r\n";
$headers .= "Reply-To: ". $from . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

$subject = "You have a message.";

$logo = 'img/logo.png';
$link = '#';

$body = "<!DOCTYPE html><html lang='en'><head><meta charset='UTF-8'><title>Express Mail</title></head><body>";
$body .= "<table style='width: 100%;'>";
$body .= "<thead style='text-align: center;'><tr><td style='border:none;' colspan='2'>";
$body .= "</td></tr></thead><tbody><tr>";
$body .= "<td style='border:none;'><strong>Name:</strong> {$name}</td>";
$body .= "<td style='border:none;'><strong>Email:</strong> {$from}</td>";
$body .= "</tr>";
$body .= "<tr><td style='border:none;'><strong>Subject:</strong> {$csubject}</td></tr>";
$body .= "<tr><td></td></tr>";
$body .= "<tr><td colspan='2' style='border:none;'><strong>Message:</strong>{$cmessage}</td></tr>";
$body .= "</tbody></table>";
$body .= "</body></html>";



if(mail($to, $subject, $body, $headers))
{
echo "<script>alert('successfully sent.'); window.location.href='contact.php';</script>";
}

?>


My Answer:
The issue might be with the way you are redirecting back to contact.php after sending the email. Instead of using window.location.href in the PHP script, you can set a session variable to store the success message and then redirect back to contact.php.

Here's how you can modify your contact_process.php file:

php
session_start();

$to = "example@example.com";
$from = $_REQUEST['email'];
$name = $_REQUEST['name'];
$csubject = $_REQUEST['subject'];
$cmessage = $_REQUEST['message'];

$headers = "From: $from";
$headers = "From: " . $from . "\r\n";
$headers .= "Reply-To: ". $from . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

$subject = "You have a message.";

$logo = 'img/logo.png';
$link = '#';

$body = "Express Mail";
$body .= "";
$body .= "";
$body .= "";
$body .= "";
$body .= "";
$body .= "";
$body .= "";
$body .= "";
$body .= "
";
$body .= "
Name: {$name}Email: {$from}
Subject: {$csubject}
Message:{$cmessage}
";
$body .= "";

if(mail($to, $subject, $body, $headers)) {
$_SESSION['success_message'] = "Email sent successfully.";
header("Location: contact.php");
exit();
}
?>


Then, in your contact.php file, you can check for the session variable and display the success message:

php
session_start();
if(isset($_SESSION['success_message'])) {
echo "";
unset($_SESSION['success_message']);
}
?>


This way, the success message will be displayed after sending the email and redirecting back to contact.php.

Rate this post

3 of 5 based on 4723 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us