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';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法