简体   繁体   English

验证电子邮件表格

[英]Validating email form

What's the best solution to make the name and email fields compulsory in my form? 什么是使我的表格中的姓名和电子邮件字段为必填项的最佳解决方案?

Can't seem to find a direct solution for my form and no expert. 似乎也找不到专家的直接解决方案,也没有专家。

I'm currently receiving loads of blank emails. 我目前收到大量空白电子邮件。

HTML code HTML代码

<form name="enq" method="post" action="mail/myphpfile.php" onSubmit="return validation();">
  <fieldset>
    <input type="text" name="name" id="name" value=""  class="input-block-level" placeholder="Name" />
    <input type="text" name="email" id="email" value="" class="input-block-level" placeholder="Email" />
    <textarea rows="11" name="message" id="message" class="input-block-level" placeholder="Comments"></textarea>
    <div class="actions">
      <input type="submit" value="Send Your Message" name="submit" id="submitButton" class="btn btn-info pull-right" title="Click here to submit your message!" />
    </div>
  </fieldset>
</form>

PHP code PHP代码

<?php
if(isset($_POST['submit']))
{
    $name = $_POST['name'];
    $email = $_POST['email'];
    $query = $_POST['message'];
    $email_from = $name.'<'.$email.'>';

    $to="name@myemailaddress.co.za";
    $subject="Enquiry from my website";
    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    $headers .= "From: ".$email_from."\r\n" . "BCC:email@email1.co.za,      email@email2.co.za, email@email3.co.za\r\n";
    $message="    
     Name: $name       
     <br>
     Email-Id:$email       
     <br>
     Message:$query";

    if(mail($to,$subject,$message,$headers))
            header("Location:../thank-you.html");
    else
        header("Location:../error.html");
        //contact:-email@myemail.co.za
}
?>

Try 尝试

  <?php
 if(isset($_POST['submit']) && !empty($_POST['email']) && !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false)
{
$name = $_POST['name'];
$email = $_POST['email'];
$query = $_POST['message'];
$email_from = $name.'<'.$email.'>';

$to="name@myemailaddress.co.za";
$subject="Enquiry from my website";
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: ".$email_from."\r\n" . "BCC:email@email1.co.za,      email@email2.co.za, email@email3.co.za\r\n";
$message="Name: $name <br>
 Email-Id:$emai<br>
 Message:$query ";

if(mail($to,$subject,$message,$headers))
 header("Location:../thank-you.html");
 else
  header("Location:../error.html");
 //contact:-email@myemail.co.za
 }

use html5 required validation like this 使用这样的html5必需的验证

<input type="text" name="name" id="name" value=""  class="input-block-level" placeholder="Name" required/>

<input type="email" name="name" id="name" value=""  class="input-block-level" placeholder="Name" required/>

In HTML5 this should work fine! 在HTML5中,这应该可以正常工作!

<input type="text" name="name" id="name" value=""  class="input-block-level" placeholder="Name" required/>

<input type="email" name="emailField" id="name" value=""  class="input-block-level" placeholder="Email" required/>
try this!!

if(isset($_POST['submit']))
{
    $email = test_input($_POST["email"]);
    if (!filter_var($email, FILTER_VALIDATE_EMAIL) && $email =="" ) {
        $emailErr = "Enter valid email."; 
    }
    else
    {
        $emailErr = "";
    }

    if( $emailErr==""){
        mail($to,$subject,$message,$headers);
        header("Location:../thank-you.html");

    }
    else
    {
        echo $emailErr;
       header("Location:../error.html");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM