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

时间:2014-03-18 06:57:11

标签: javascript php jquery ajax forms

我是PHP的新手,在添加电子邮件验证时遇到问题,然后在按下提交按钮后隐藏表单。我有一个error.php,thankyou.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中的任何其他信息,请告诉我。 提前谢谢!

1 个答案:

答案 0 :(得分:1)

要在php中验证电子邮件,请按this link

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';
}