发送电子邮件时验证错误

时间:2014-12-17 09:46:50

标签: javascript php email

我想发送电子邮件。当我点击email按钮并刷新页面时,它给我验证错误'字段是必填的',即使我输入了正确的电子邮件ID。如果我输入了正确的电子邮件ID,那么它也会出错。 我不知道我哪里错了。任何人都可以告诉我。这是我的PHP代码:

function validation1()
{
    document.getElementById("emailid").innerHTML="";

    var emailpattern=/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,4}$/;
    if(document.form1.email.value=="")
    {
        document.getElementById("emailid").innerHTML="fields are mandetory";
        document.form1.email.focus();
        return false;
    }
    else if(!emailpattern.test(document.form1.email.value))
    {
        document.getElementById("emailid").innerHTML="enter valid id";
        document.form1.email.focus();
        return false;
    }
return true;
}

<?php include 'send-email.php';?>
<form name="form1" method="post">
<input type="submit" name="email" value="email" onclick="return validation1()" />
<input type="text" size="50" name="email" id="email" placeholder="*************" />
<span id="emailid"></span>
</form>

send-email.php代码

<?php include 'connection.php'; ?>
<?php 
if(isset($_POST['submit'])) {
@$email=$_POST["email"];

$email=strip_tags($_POST["email"]);
$email=mysqli_real_escape_string($con,trim($email));

$to="sa@vs.com";
$to=mysqli_real_escape_string($con,trim($to));
$headers="From:".$email;
$message="Interested in VAPT";
$subject="email id";
if(mail($to,$subject,$message,$headers))
{
    $email="'".$email."'";

    $sql="insert into email_to_admin values($email)";
    if (!mysqli_query($con,$sql))
        {
            die('Error: ' . mysqli_error($con));
        }
}

mysqli_close($con);
}
?>

3 个答案:

答案 0 :(得分:0)

您没有if()语句来控制邮件的发送时间。一旦页面加载,您立即邮件。使用

if(isset($_POST['email'])) {}

答案 1 :(得分:0)

     if(isset($_POST["email"]) && isset($_POST["email"] !="" )
     {

             //send the email

     }

就我个人而言,我建议也放置一个反垃圾邮件验证码,这样就可以确保新的rendom值不一样

由于您还有另一个可以帮助您的步骤,您还可以在email_to_admin MySQL表中标记电子邮件地址以及其他一些值,例如主题唯一,如果插入了记录,则继续

  $sql="insert into email_to_admin values($email , $subject)";
  .....................
  $num = @mysql_affected_rows();
  if($num > 0)
  {
      //semd the email
  }

答案 2 :(得分:-3)

请尝试以下邮件: -

if($_POST["email"]){  // Add this if condition so that your mail will be send when button gets clicked.

$to="sa@vs.com";
$to=mysqli_real_escape_string($con,trim($to));
$headers="From:".$email;
$message="Interested in VAPT";
$subject="email id";
if(mail($to,$subject,$message,$headers))
{
    $email="'".$email."'";

    $sql="insert into email_to_admin values($email)";
    if (!mysqli_query($con,$sql))
        {
            die('Error: ' . mysqli_error($con));
        }
}


}