droxy80248 2014-03-18 06:57
浏览 48
已采纳

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

I am new to PHP and am having trouble with adding email validation and then hiding the form after pressing the submit button. I have an error.php, thankyou.php and formmail.php. The PHP code below is that of 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. And as far as hiding the form after submit, I really don't know what to do with that.

Here is my 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
// 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

" .
    "Email: $email_from

" .
    "Comment: $comments

"
;

// 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. Thanks in advance!

  • 写回答

1条回答 默认 最新

  • dongtangxi1584 2014-03-18 07:03
    关注

    To validate email in php, follow this link,

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

    To hide the form, using jquery,

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

    or using pure javascript,

    Change HTML,

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

    and JS

    function myFunction(){
        var form = document.getElementById("ajaxsubmit");
        form.style.display = 'none';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么