需要帮助添加电子邮件验证和隐藏表单后提交


Need help adding Email Validation and hiding form after submit

我是PHP新手,我在添加电子邮件验证时遇到麻烦,然后在按提交按钮后隐藏表单。我有一个错误。php,谢谢。php和formmail。php。下面的PHP代码是formmail.php的代码。我只是不知道该为这段特定代码的电子邮件验证写些什么。我试过从其他网站复制一些PHP代码,但它只是不匹配我的代码。至于在提交后隐藏表单,我真的不知道该怎么做。

这是我的HTML:
    <div id="contact">
        <div id="contact-left">
        <form id="ajaxsubmit" action="formmail.php" method="post">
<div class="form">
    <div class="formblock">
    <h2>Name</h2>
    <input name="name" type="text" class="required txt" id="name" />
    </div>
    <div class="formblock">
    <h2>Email</h2>
    <input name="email" class="required txt" type="text" id="email" />
    </div>
    <div class="formblock">
    <h2>Message</h2>
    <textarea name="comments" cols="" rows="" id="comments"></textarea>
    </div>
    <input name="Submit" type="submit" class="subbtn" value="Submit"  />
    <div id="message"></div>
    </form>
        </div>
    </div>

这是我的PHP:

    <?php
// Insert your email/web addresses and correct paths
$mailto = 'myemail@email.com' ;
$from = "http://website.com" ;
$formurl = "http://website.com/formmail.php" ;
$errorurl = "http://website.com/error.php" ;
$thankyouurl = "http://website.com/thankyou.php" ;
// Place Your Form info here...
$firstname = ($_POST['name']);
$email_from = ($_POST['email']);
$comments = ($_POST['comments']);
// Check If Empty
if (empty($firstname)) {
   header( "Location: $errorurl" );
   exit ;
}
// Add more Validation/Cleaning here...

// Place your Corresponding info here...
$message =
    "Name: $firstname'n'n" .
    "Email: $email_from'n'n" .
    "Comment: $comments'n'n"
;
// Leave Alone
mail($mailto, $from, $message,
    "From: '"$name'" <$email>" . $headersep . "Reply-To: '"$name'" <$email>" . $headersep );
header( "Location: $thankyouurl" );
exit ;
?>

请让我知道,如果你可以帮助我或需要任何其他信息从我的html。提前感谢!

要在php中验证电子邮件,请遵循此链接,

if (filter_var($email_from, FILTER_VALIDATE_EMAIL)) {
    echo "This ($email_from) email address is considered valid.";
}

使用jquery隐藏表单

$(document).on('submit', '#ajaxsubmit',function(){
   $(this).hide();
});

或使用纯javascript,

更改HTML,

<form id="ajaxsubmit" action="formmail.php" method="post" onsubmit="myFunction()">

和JS

function myFunction(){
    var form = document.getElementById("ajaxsubmit");
    form.style.display = 'none';
}