简体   繁体   English

自定义php错误消息fadein

[英]custom php error message fadein

I have written the following code as part of the user signup process for my website. 我已经编写了以下代码作为我的网站的用户注册过程的一部分。 Obviously once the user has filled out their email and clicks submit the form data is sent through this: 显然,一旦用户填写了他们的电子邮件并点击提交,表单数据将通过以下方式发送:

<?php

if(isset($_POST['email'])){

$email_from = $_POST['email'];

function died($error) {
    echo $error;
    die();
}

if(!isset($_POST['email'])) {
    died($error);
}

$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
    if(!preg_match($email_exp,$email_from)) {

  $error_message .= ' <script type="text/javascript">
  alert("The email address you entered does not appear to be valid");
  window.location = "http://www.domain.com/";
  </script> ' ;
    }

if(strlen($error_message) > 0) {
    died($error_message);
}

etc, etc, etc....

What i would really appreciate some help with is this bit of script: 我真的很感激一些帮助就是这个脚本:

$error_message .= ' <script type="text/javascript">
  alert("The email address you entered does not appear to be valid");
  window.location = "http://www.domain.com/";
  </script> ' ;
    }

How can create an error message that just fades in (and out on click) on the html page, rather than just being a js alert infront of the blank white php page before redirecting to the html page?? 如何在html页面上创建一个只是淡入(和点击)的错误消息,而不是在重定向到html页面之前只是在空白的白色php页面前面的js警报?

Thank you! 谢谢!

How about using jQuery's fadeIn function? 如何使用jQuery的fadeIn函数? http://api.jquery.com/fadeIn/ In case you don't know how to use jQuery, you should simply link your html page to jQuery's .js script and use the functions as you need. http://api.jquery.com/fadeIn/如果你不知道如何使用jQuery,你应该简单地将你的html页面链接到jQuery的.js脚本并根据需要使用这些函数。

Update : 更新

To answer your comment. 回答你的评论。 According to a discussion in http://stackoverflow.com/questions/3708850/is-there-an-onload-event-for-input-elements , here is what you may do: 根据http://stackoverflow.com/questions/3708850/is-there-an-onload-event-for-input-elements的讨论,您可以执行以下操作:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <script type="text/javascript" src="jquery-1.5.1.min.js"></script>
    <script type="text/javascript">
    function test() { 
        alert('test');
    }
    </script>
</head>
<body >
    <?php 
    if($condition) { 
        print '<input type="hidden" />
    <script>
    test();
    </script>';
    }

    ?>


</body>

Then, you can do whatever you want in test() , eg, calling the fadeIn. 然后,您可以在test()执行任何操作,例如,调用fadeIn。 Also, you can specify your desired condition in the PHP if-statement. 此外,您可以在PHP if语句中指定所需的条件。

<span class="fadeInMessage" style="display:none;">"The email address you entered does not appear to be valid"</span>

This would start off invisible. 这将开始隐形。 Then you would write the following code: 然后你会写下面的代码:

$(function(){
    $('.fadeInMessage').fadeIn();
});

I agree with the other two answers; 我同意其他两个答案; you should use jQuery's fadeIn function. 你应该使用jQuery的fadeIn函数。 However, if you don't want to or can't use jQuery, this should get you started on a pure Javascript implementation: 但是,如果您不想或不能使用jQuery,这应该让您开始使用纯Javascript实现:

HTML: HTML:

<div id="message">Hello world!</div>​

Javascript: 使用Javascript:

var msg = document.getElementById( 'message' );
msg.style.opacity = 0;

function fadeIn() {
    if( msg.style.opacity < 1 ) {
        msg.style.opacity = parseFloat(msg.style.opacity) + 0.01;        
        setTimeout( fadeIn, 25 );
    }
}

function fadeOut() {
    if( msg.style.opacity > 0 ) {
        msg.style.opacity = parseFloat(msg.style.opacity) - 0.01;
        setTimeout( fadeOut, 25 );
    }
}        

fadeIn();

msg.onclick = function() { fadeOut(); }

​ Here's a jsFiddle demonstrating: http://jsfiddle.net/GgYS8/ 这是一个jsFiddle演示: http//jsfiddle.net/GgYS8/

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

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