0

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;

?>
  • 1
    Does this answer your question? [Using if(isset($\_POST\['submit'\])) to not display echo when script is open is not working](https://stackoverflow.com/questions/7775512/using-ifisset-postsubmit-to-not-display-echo-when-script-is-open-is-not) – Martheen Dec 30 '20 at 09:44
  • I added name attribute but still not working –  Dec 30 '20 at 09:54
  • As in, not even one of the alerts? Try dumping the post values https://stackoverflow.com/questions/9332718/how-do-i-print-all-post-results-when-a-form-is-submitted – Martheen Dec 30 '20 at 09:57
  • yes none of the alerts is working –  Dec 30 '20 at 10:05
  • Dump the post values – Martheen Dec 30 '20 at 10:06

3 Answers3

0

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>
  • I added name attribute but still not working –  Dec 30 '20 at 09:55
  • Strange .. Could you please insert the code of the contact.php in a comment or update it above? Try to use a standard echo "it worked" instead of the alert just in case you have a popup blocker and insert a temporary echo in both parts of the if condition to see which branch is taken. – Alexander Dobernig Dec 30 '20 at 09:59
  • I put exactly your code adding the name attribute and only changed the email address and put it on one of my servers and it worked perfectly. and i just got the email ;-) MAYBE there is a problem with the mail.php script so that aborts with an error 500? and nothing is displayed at all?? – Alexander Dobernig Dec 30 '20 at 10:16
  • I edited my post to insert some code to reduce the numbers of possible problems. please try this. possible problems: on client or server Javascript popups are not allowed - Popup Blocker /AdBlocker - JS is not allowed - mail() does not work - If possible it look into your server log to see if you have any fatal errors – Alexander Dobernig Dec 30 '20 at 10:26
  • 1
    It is showing:- submit is not set after the if condition –  Dec 30 '20 at 16:08
  • Please suggest something since it is working on your system, it would be a great help –  Dec 30 '20 at 16:12
  • @Soumya I have updated my answer above yesterday and included the php code you can use on your webpage to find out the problem. just copy paste it as the mail.php file and tell us what is displayed on your screen. it can be found below **try this code as mail.php and tell us what happens ;-) ** – Alexander Dobernig Dec 31 '20 at 16:15
  • Sorry - did not see your reply of yesterday: If you get this messages displayed then you still have a problem in the contact.php file. Please update the complete code of the current contact.php file in your original question. or comment here with the line containing the button. – Alexander Dobernig Dec 31 '20 at 16:22
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/226671/discussion-between-alexander-dobernig-and-soumya). – Alexander Dobernig Dec 31 '20 at 16:26
0

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>";
  }
}
?>
0

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">
Zoheib Ali
  • 21
  • 3