I am busy with an email form using PHP and raw HTML and CSS. I am trying to get a confirmation that the email was sent back to the user.
HTML Code:
<div>
<form class="input-form2" action="mail.php" method="post">
<h3 class="form-heading2 text-bebas">Send me an email</h3>
<div class="input-field-row2">
<p class="form-label2">Name:</p>
<input type="text" name="name" placeholder="Full Name..." required>
</div>
<div class="input-field-row2">
<p class="form-label2">Email:</p>
<input class="text-mont" type="text" name="email" placeholder="Email..." required>
</div>
<div class="input-field-row2">
<p class="form-label2">Subject:</p>
<input class="text-mont" type="text" name="subject" placeholder="Subject..." required>
</div>
<div class="flex-row2">
<p class="form-label2" style="height: 40px;">Message:</p>
<textarea class="textarea-class text-mont" name="message" placeholder="Message..." required></textarea>
</div>
<div style="width: 100%; justify-content: space-between; margin-top: 40px;" class="flex-row2">
<div class="confirm-box">
<input class="largerCheckbox" type="checkbox" id="confirm" name="confirm" required>
<p>I'm not a robot</p>
</div>
<div class="email-sent">Email Sent</div>
<button class="form-button text-mont-bold" type="submit" name="submit">Send</button>
</div>
</form>
</div>
<?php include 'mail.php';?>
PHP Code (file is mail.php):
<?php
if(isset($_POST['submit'])) {
$name = $_POST['name'];
$subject = $_POST['subject'];
$mailFrom = $_POST['email'];
$message = $_POST['message'];
$confirm = $_POST['confirm'];
if($confirm == false) {
header('Location: https://sleekwebdesign.co.za?sendingFailed');
}
$array = explode( "@" , $mailFrom);
$mailTo = "piet@gmail.com";
$headers = 'From: ' . $mailFrom . "\r\n" .
'X-Mailer: PHP/' . phpversion();
echo "headers: ";
echo $headers;
echo "<br>";
if( mail($mailTo, $subject, $message, $headers) ) {
echo "mail sent";
// header('Location: https://sleekwebdesign.co.za?mailsent');
// alert("Email sent");
} else {
echo "mail not sent";
}
}
If the email was sent correctly I want some confirmation, either by displaying the email-sent div. So my question is how do I bind that HTML element to a PHP variable so when the variable is true I show the element.
You may also provide a different method. I have tried using
alert("Email sent");
header('Location: https://sleekwebdesign.co.za?mailsent');
but the problem here is that it is redirected before doing the alert. So is there an easier way to apply this?