The below HTML and PHP code executes as expected in Google Chrome - enter the requested data into the form and press send, then a message is sent to an email address and the page is refreshed.
However, the same expected behaviour does not occur in Safari - once the 'send' button is clicked the user is brought to a blank white page (the email.php file).
Can anyone tell me why it does not do as expected in Safari?
HTML Code:
<form method="POST" id="contactForm" name="contactForm" novalidate="novalidate" action="email.php" onsubmit="this.submit(); this.reset(); return false;">
<div >
<!-- Name Input -->
<div >
<div >
<label >Name:</label>
</div>
</div>
<div >
<div >
<input type="text" name="name" id="name">
</div>
</div>
<!-- Email Input -->
<div >
<div >
<label >Email:</label>
</div>
</div>
<div >
<div >
<input type="email" name="email" id="email">
</div>
</div>
<!-- Message Input -->
<div >
<div >
<label >Message:</label>
</div>
</div>
<div >
<div >
<textarea name="message" id="message" cols="30" rows="6"></textarea>
</div>
</div>
</div>
</div>
</div>
<!-- Submit Button -->
<div >
<div >
<button type="submit" >Send</button>
<div ></div>
</div>
</div>
</div>
</form>
PHP Code:
$errors = [];
$errorMessage = '';
if (!empty($_POST)) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
if (empty($name)) {
$errors[] = 'Name is empty';
}
if (empty($email)) {
$errors[] = 'Email is empty';
} else if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors[] = 'Email is invalid';
}
if (empty($message)) {
$errors[] = 'Message is empty';
}
if (empty($errors)) {
$toEmail = '[email protected]';
$emailSubject = 'New email from your contant form';
$headers = ['From' => $email, 'Reply-To' => $email, 'Content-type' => 'text/html; charset=iso-8859-1'];
$bodyParagraphs = ["Name: {$name}", "Email: {$email}", "Message:", $message];
$body = join('<br>', $bodyParagraphs);
if (mail($toEmail, $emailSubject, $body, $headers)) {
header('Location: https://cillinlyons.me');
} else {
$errorMessage = 'Oops, something went wrong. Please try again later';
}
} else {
$allErrors = join('<br/>', $errors);
$errorMessage = "<p style='color: red;'>{$allErrors}</p>";
}
}
?>
CodePudding user response:
you need a name for your button to set it with php
<button type="submit" name="submit">Send</button>
and use array_push() to have all errors in a array ,
i used isset() for check the submit is clicked or not.
<?php
$errors = [];
$errorMessage = '';
if(isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
if (empty($name)) {
array_push($errors, 'Name is empty');
}
if (empty($email)) {
array_push($errors, 'Email is empty');
} else if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
array_push($errors, 'Email is invalid');
}
if (empty($message)) {
array_push($errors, 'Message is empty');
}
if (empty($errors)) {
$toEmail = '[email protected]';
$emailSubject = 'New email from your contant form';
$headers = ['From' => $email, 'Reply-To' => $email, 'Content-type' => 'text/html; charset=iso-8859-1'];
$bodyParagraphs = ["Name: {$name}", "Email: {$email}", "Message:", $message];
$body = join('<br>', $bodyParagraphs);
if (mail($toEmail, $emailSubject, $body, $headers)) {
header('Location: https://cillinlyons.me');
} else {
$errorMessage = 'Oops, something went wrong. Please try again later';
}
} else {
foreach ($errors as $error)
echo "<p style='color: red;'>{$error}</p>";
}}
i tested this on safari and its working