I was working on a message form that sends a copy of the message to the email address when it is submitted. It worked fine when the form action
directed to a different php file. But when I directed the form to submit itself to the same file the form is in it sent the message to the database but didn't send the email. It would be great if someone could tell me why this is happening. Here's the code for contact.php
file:
<div class="form-wrap">
<form class="form-flex" action="contact.php" method="post">
<label for="">Send us a message</label>
<input type="text" name="name" type="text" placeholder="Name">
<input type="text" name="email" type="text" placeholder="Email">
<textarea name="message" id="message" placeholder="Message"></textarea>
<button class="btn-big" name="submit" type="submit">Send</button>
<?php
if(isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$connection = mysqli_connect('host', 'user', 'password', 'messages');
$name = mysqli_real_escape_string($connection, $name);
$email = mysqli_real_escape_string($connection, $email);
$message = mysqli_real_escape_string($connection, $message);
if($connection) {
$query = "INSERT INTO msg(name,email,message) ";
$query .= "VALUES ('$name','$email','$message')";
mysqli_query($connection, $query);
$email_subject = 'New Message';
$email_body = $message;
$to = 'myemail@gmail.com';
mail($to, $email_subject, $email_body);
echo "<p class='form'>Message sent!</p>";
} else {
die("<p class='form'>Database connection failed</p>");
}
}
?>
</form>
</div>