I have this form:
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<div>
<span><label>Name</label></span>
<span><input name="userName" type="text" class="textbox"></span>
</div>
<div>
<span><label>Email</label></span>
<span><input name="userEmail" type="text" class="textbox"></span>
</div>
<div>
<span><label>Phone</label></span>
<span><input name="userPhone" type="text" class="textbox"></span>
</div>
<div>
<span><label>Subject</label></span>
<span><textarea name="userMsg"> </textarea></span>
</div>
<div>
<span><input type="submit" value="Send!"></span>
</div>
</form>
And the following PHP code on the same page because the action
is <?php echo $_SERVER['PHP_SELF']; ?>
:
<?php
if(isset($_POST)) {
$name = htmlspecialchars($_POST['userName']);
$email = htmlspecialchars($_POST['userEmail']);
$phone = htmlspecialchars($_POST['userPhone']);
$message = htmlspecialchars($_POST['userMsg'] . $phone);
$message = wordwrap($message, 70, "\r\n");
$to = 'myEmail';
$subject = 'subject';
$headers = "From: $email" . "\r\n" .
"Reply-To: $email" . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
}
?>
But when a user hasn't submitted the form and just visits the contact form page, I get a blank email.
How can I fix this?