Hi There, Happy New Year to you all out there.
I have a question, or rather an ask, Can anyone help me to get this working?
I would just like to have website visitors to fill out the subscribe form and get it to send me an email with the data so that I can then add it to the mailing list.
I get a lot of spam hence me trying to use the validation side a bit more, I did a lot of searching and tried and tried but I can not get it to work.
The validation seems to do it's job but sending the email when all is ok, does not work. It sends it without waiting for all data to be filled in proper.
I do not want the email to be sent until all is verified and with no errors.
Your help is appreciated.
[ATTACH=CONFIG]5431[/ATTACH]
<?php
// define variables and set to empty values
$nameErr = $emailErr = $captchaErr ="";
$name = $email = $captcha = "";
$answer ="6";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "<span class='sub_error'>Name is required.</span>";
} else {
$name = test_input($_POST["name"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "<span class='sub_error'>Only letters and white space allowed.</span>";
}
}
if (empty($_POST["email"])) {
$emailErr = "<span class='sub_error'>Email is required.</span>";
} else {
$email = test_input($_POST["email"]);
// check if e-mail address is well-formed
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "<span class='sub_error'>Invalid email format.</span>";
}
}
if (empty($_POST["captcha"]))
{
$captchaErr = "<span class='sub_error'>Please add up</span>.<br>";
}
else {
$captcha = test_input($_POST["captcha"]);
}
if ($captcha !== $answer)
{
$captchaErr = "<span class='sub_error'>3 + 3 = !Wrong Answer</span>.<br>";
}
else
{
$captcha = test_input($_POST["captcha"]);
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
$to="myEmail@mywebsite.com";
$subject = "PL NL Subs";
$from = $email;
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= "From: $from\r\n";
$message = 'New Sub for Peter Lynn';
$message = "Their Email= $email.\r\n\r\n";
$message .= "Their Name= $name\r\n\r\n";
mail($to,$subject,$message,$headers,"-f $from");
if (mail) {
echo "email has been sent";
}
else {
echo "email has NOT been sent.";
}
?>
<div class="subform">
<div align="center"><div class="img"><img src="images/octopus2.png"></div></div>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<div class="form_input_name"><input type="text" name="name" value="<?php echo $name;?>" placeholder="Your Name"><?php echo $nameErr;?></div>
<div class="form_input_email"><input type="text" name="email" value="<?php echo $email;?>" placeholder="Your Email"><?php echo $emailErr;?></div>
<div class="form_input_captcha"><input type="text" name="captcha" value="<?php echo $captcha;?>" placeholder="Are you human? 3 + 3 = ?"/><?php echo $captchaErr;?></div>
<input type="submit" name="submit" value="Subscribe" class="subscr">
</form>
</div>
<script type="text/javascript">
// ref: http://diveintohtml5.org/detect.html
function supports_input_placeholder()
{
var i = document.createElement('input');
return 'placeholder' in i;
}
if(!supports_input_placeholder()) {
var fields = document.getElementsByTagName('INPUT');
for(var i=0; i < fields.length; i++) {
if(fields[i].hasAttribute('placeholder')) {
fields[i].defaultValue = fields[i].getAttribute('placeholder');
fields[i].onfocus = function() { if(this.value == this.defaultValue) this.value = ''; }
fields[i].onblur = function() { if(this.value == '') this.value = this.defaultValue; }
}
}
}
</script>
Form.png