This is my first time trying to use php in a website and posting a question to stackoverflow. I hope I haven't posted too much code.
The goal is to have the results of a foreach
loop display an array's content in the form's html. I'm have been able to display one error message, but not more than one when several exist. Currently the code returns 'Array' when submitted with errors.
<?php
$to = "companyname@email.com";
$subject = "Contact Form";
if (isset($_POST["submit"])) {
$nam = strip_tags($_POST["name"]);
$ema = htmlspecialchars($_POST['email']);
$msg = strip_tags($_POST["message"]);
$error_msg=array(); //initiate array, not required
if (empty($nam) || !preg_match("/^[\s.'\-\pL]{1,60}$/u", $nam)) {
$error_msg[] = "<p>Please fill in your name with only letters, spaces and basic punctuation (. - ')</p>";
}
if (empty($ema) || !filter_var($ema, FILTER_VALIDATE_EMAIL)) {
$error_msg[] = "<p>Your email must have a valid format, such as emailname@mailhost.com</p>";
}
$limit = 1500;
if (empty($msg) || !preg_match("/^[0-9\/\-\_\s'\(\)\@!\?\.,:;\pL]+$/u", $msg) || (strlen($msg) > $limit)) {
$error_msg[] = "<p>The Message field must contain only letters, digits, spaces and basic punctuation ( ' - , . : ; / and parentheses), and has a limit of 1500 characters</p>";
}
$body =
"Name of sender: $nam
" .
"Email of sender: $ema
" .
"Message:
" .
"$msg" ;
$body = wordwrap($body, 70);
$headers = "Reply-To: $nam <$ema>
";
$headers .= "Return-Path: <$ema>
";
$headers .= "From: $nam <$ema>
";
$headers .= "MIME-Version: 1.0
";
$headers .= "X-Sender: $ema
";
$headers .= "Content-type: text/plain; charset=iso-8859-1
";
$headers .= "X-Priority: 3
";
$headers .= "X-Mailer: PHP" . phpversion() . "
";
if ($error_msg) {
foreach ($error_msg as $error_output) {
$error_msg_list .= '<div class="error">' . $error_output . '</div>';
}
// if no errors, send the email
} else if (!$error_msg) {
mail ($to, $subject, $body, $headers);
$success_output = "Your message has been sent. We will get back to you shortly.";
$_POST = array(); //clears the form data after valid form submission
}
} //end isset
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Contact Us</title>
</head>
<body>
<div class="content">
<h1>Contact Us</h1>
<form method="post" action="index.php">
<?php
if (isset($error_msg_list)) {
echo $error_msg_list;
} else {
echo "<div class='success'>" . $success_output . "</div>";}
?>
<div>
<label for="name">Name</label>
<input name="name" type="text" id="name" placeholder="your name" value="<?php if (isset($_POST["name"])) {echo $nam;} ?>">
</div>
<div>
<label for="email">Email Address</label>
<input name="email" type="email" id="email" placeholder="your email address" value="<?php if (isset($_POST["email"])) {echo $ema;} ?>">
</div>
<div>
<label for="message">Message</label>
<textarea name="message" rows="10" id="message"><?php if (isset($_POST["message"])) {echo $msg;} ?></textarea>
</div>
<div>
<input type="submit" name="submit" value="Send">
</div>
</form>
</div>
</body>
</html>