简体   繁体   English

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

[英]Need help adding Email Validation and hiding form after submit

I am new to PHP and am having trouble with adding email validation and then hiding the form after pressing the submit button. 我是PHP新手,在添加电子邮件验证时遇到麻烦,然后在按下“提交”按钮后隐藏了表单。 I have an error.php, thankyou.php and formmail.php. 我有一个error.php,thankyou.php和formmail.php。 The PHP code below is that of formmail.php. 下面的PHP代码是formmail.php。 I just don't know what to write for email validation for this specific code. 我只是不知道为该特定代码的电子邮件验证写什么。 I've tried copying some PHP codes from other sites but it just doesn't match up with my code. 我试过从其他站点复制一些PHP代码,但它与我的代码不匹配。 And as far as hiding the form after submit, I really don't know what to do with that. 至于提交后隐藏表单,我真的不知道该怎么办。

Here is my HTML: 这是我的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>

Here is my PHP: 这是我的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 ;

?>

Please let me know if you can assist me or need any other information from my html. 请让我知道您是否可以帮助我或需要我的html中的任何其他信息。 Thanks in advance! 提前致谢!

To validate email in php, follow this link , 要验证php中的电子邮件,请点击此链接

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

To hide the form, using jquery, 要使用jquery隐藏表单,

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

or using pure javascript, 或使用纯JavaScript,

Change HTML, 更改HTML,

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

and JS 和JS

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

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

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