简体   繁体   中英

Alert message on sending email not showing up

On sending the email by clicking the send message button an alert box should show up, but it is not coming up neither it is showing inquiry sent successfully nor it is showing email not sent I don't know where I'm going wrong. Please help me resolve this issue. Any help will be highly appreciable. I'm attaching herewith a part of my contact.php code and my mail.php code. Thank You

contact.php

<form id="contact-form" method="POST" action="mail.php">
                    <div class="row">
                        <div class="col-md-4">
                            <div class="form-group">
                                <label>Name</label>
                                <input class="form-control" name="name" id="name" placeholder="" type="text" required>
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="form-group">
                                <label>Email</label>
                                <input class="form-control" name="email" id="email" placeholder="" type="email" required>
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="form-group">
                                <label>Subject</label>
                                <input class="form-control" name="subject" id="subject" placeholder="" required>
                            </div>
                        </div>
                    </div>
                    <div class="form-group">
                        <label>Message</label>
                        <textarea class="form-control" name="message" id="message" placeholder="" rows="10" required></textarea>
                    </div>
                    <div class="text-right"><br>
                    
                        <button class="btn btn-primary solid blank button"  id="btn" type="submit" value="submit" name="submit">Send Message</button>
                    
                    </div>
    
                </form>

mail.php

<?php
if (isset($_POST['submit'])){

$to = "contact@imatrixautomation.com";
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$message .="\r\n from: $email";


if(mail ($to, $subject, $name,  $message)){
    echo "<script>alert('Enquiry sent successfully!');</script>";
   
}
    else
    {
    echo "<script>alert('Mail was not sent. Please try again later');</script>";
    }
}


header('Location: https://imatrixautomation.com/contact.php');
exit;

?>

In the contact.php file

name="submit" is missing for the submit button.

This line should be:

<button class="btn btn-primary solid blank button"  id="btn" type="submit" name="submit" value="submit">Send Message</button>
                

Explanation: Currently the

if (isset($_POST['submit'])){   

always evaluates to FALSE as POST['submit'] is empty so it continues at the header()...

################

try this code as mail.php and tell us what happens;-)

<?php
if (isset($_POST['submit'])){
    
    echo "submit is set to {$_POST['submit']} and now we send the email<br>";

$to = "contact@imatrixautomation.com"; 
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$message .="\r\n from: $email";


if(mail ($to, $subject, $name,  $message)){
    echo "Enquiry sent successfully!<br>";
   
}
    else
    {
    echo "Mail was not sent. Please try again later<br>";
    }
} else{

echo"submit is not set<br>"; 
}

echo "after the if condition";


exit;

?> 

Here i have the new contact.php file - just copy paste this

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Unbenanntes Dokument</title>
</head>

<body>
    
<form id="contact-form" method="POST" action="mail.php">
                    <div class="row">
                        <div class="col-md-4">
                            <div class="form-group">
                                <label>Name</label>
                                <input class="form-control" name="name" id="name" placeholder="" type="text" required>
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="form-group">
                                <label>Email</label>
                                <input class="form-control" name="email" id="email" placeholder="" type="email" required>
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="form-group">
                                <label>Subject</label>
                                <input class="form-control" name="subject" id="subject" placeholder="" required>
                            </div>
                        </div>
                    </div>
                    <div class="form-group">
                        <label>Message</label>
                        <textarea class="form-control" name="message" id="message" placeholder="" rows="10" required></textarea>
                    </div>
                    <div class="text-right"><br>
                    
                        <button class="btn btn-primary solid blank button"  id="btn" type="submit" name ="submit" value="submit">Send Message</button>
                    
                    </div>
    
                </form>    
    
</body>
</html>

you should add name to your button

<button class="btn btn-primary solid blank button"  id="btn" name="submit" type="submit" value="submit">Send Message</button>

also as you're redirecting by changing header it won't show you the alert as alert was on different page.

try this code

<form id="contact-form" method="POST" action="">
<div class="row">
    <div class="col-md-4">
        <div class="form-group">
            <label>Name</label>
            <input class="form-control" name="name" id="name" placeholder="" type="text" required>
        </div>
    </div>
    <div class="col-md-4">
        <div class="form-group">
            <label>Email</label>
            <input class="form-control" name="email" id="email" placeholder="" type="email" required>
        </div>
    </div>
    <div class="col-md-4">
        <div class="form-group">
            <label>Subject</label>
            <input class="form-control" name="subject" id="subject" placeholder="" required>
        </div>
    </div>
</div>
<div class="form-group">
    <label>Message</label>
    <textarea class="form-control" name="message" id="message" placeholder="" rows="10" required></textarea>
</div>
<div class="text-right"><br>
    
    <button class="btn btn-primary solid blank button"  id="btn" name="submit" type="submit" value="submit">Send Message</button>

</div>
</form>

and below this write your php code in same file

<?php
if(isset($_POST['submit'])) {

$to = "contact@imatrixautomation.com";
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$message .= "\r\n from: $email";


if(mail($to, $subject, $name, $message)) {
    echo "<script>alert('Enquiry sent successfully!');</script>";
    
}
else {
    echo "<script>alert('Mail was not sent. Please try again later'); 
          </script>";
  }
}
?>

Replace

<button class="btn btn-primary solid blank button"  id="btn" type="submit" value="submit" name="submit">Send Message</button>

To

<input class="btn btn-primary solid blank button"  id="btn" type="submit" value="submit" name="submit">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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