<?php
// Function that validates your formfields and generate errors
function validate($name, $email)
{
$errors = array();
// validation for name
if(strlen($name) < 2)
$errors[] = 'You did not enter a name.';
// validation for email
if(!strlen($email))
$errors[] = 'non-valid email.';
else if(!filter_var($email, FILTER_VALIDATE_EMAIL))
$errors[] = 'valid email.';
// return array with errors
return $errors;
}
// initialization of variables with default values
$name = '';
$email = '';
$errors = array();
// if the form has been submited
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
// overwrite the variables with the $_POST elements
$name = $_POST['name'];
$email = $_POST['email'];
The Code I'm trying to add (
$recipient = 'example@gmail.com';
$subject = 'Simple Forms';
$mailheader = 'From: $email \r\n';
$formcontent = '$name, $email';
)
// validate the variables
$errors = validate($name, $email);
// If there aren't any errors redirect to another page
if(!count($errors))
{
// Here comes the place to send an email!!!
The code I'm trying to add (
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
)
header('Location: contact.html');
exit;
}
}
?>
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>a titel</title>
</head>
<body>
<ul id="errors">
<?php
foreach($errors as $error)
echo '<li>' . $error . '</li>';
?>
</ul>
<form action="" method="post">
<input type="text" name="name" value="<?php echo $name; ?>" />
<input type="email" name="email" value="<?php echo $email; ?>" />
<button type="submit">Submit</button>
</form>
</body>
</html>
The email is sending to the recipient correctly and with the correct subject line but it's from $email rn and the body of the email reads: $name, $email.
example email)
from: $email rn to: example@gmail.com Simple Forms
$name, $email
It's very close to working but I'm not sure how to use the $name and $email variable correctly. Any advice would be greatly appreciated.