【问题标题】:jQuery fadeOut() not working in PHP echojQuery fadeOut() 在 PHP 回显中不起作用
【发布时间】:2013-02-03 08:31:01
【问题描述】:

我一直在尝试、搜索、尝试、研究一整夜,以了解如何在 PHP 登录成功后让页面淡出,然后在成功淡出后重定向到受保护的页面。

这是我目前所拥有的,尽管它是我一直在尝试的众多更改中的最新一项:

链接: ournewlife.com/002/login.php

和代码:

<?php

session_start();

if ($_GET['login']) {
     // Only load the code below if the GET
     // variable 'login' is set. You will
     // set this when you submit the form

if (in_array($_POST['entryphrase'], array('enter', 'goin', 'entrezvous', 'opensesame'))) {
         // Load code below if both username
         // and password submitted are correct

         $_SESSION['loggedin'] = 1;
          // Set session variable

          echo "<script type='text/javascript'>";
          echo "$('#ournewform').fadeOut(2222);";  
          echo "</script>";

//        header("Location: protected.php");
         exit;
         // Redirect to a protected page

     } else echo "";
     // Otherwise, echo the error message

}

?>

<form action="?login=1" method="post" id="ournewform">
    <input type="text" id="ournewlogin" name="entryphrase" class="fontface" />
</form>

我还尝试在从单独的 .js 文件中按下回车键后加载转换,但是 PHP 不会更新会话,并且当它重定向时,登录尚未得到验证,并且会将我吐回登录页面。

这里有什么我遗漏的吗?我只是想在有效登录后淡出页面,然后成功进入受保护页面。

我现在注释掉了重定向行,以便我可以在 jQuery 上工作。

【问题讨论】:

  • 回显出来的js代码永远不会被执行;这是因为您正在回显脚本数据,然后尝试重定向。此外,它会给出一个名为“警告:无法修改标头信息 - 标头已发送”的警告

标签: php jquery fadeout


【解决方案1】:

您缺少 jquery 插件和文档就绪处理程序:

      echo "<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js'></script>";
      echo "<script type='text/javascript'>";
      echo "$(function(){";
      echo "$('#ournewform').fadeOut(2222);";  
      echo "});";
      echo "</script>";

【讨论】:

  • 在回显上述代码的行之后退出后,代码将如何到达实际定义表单的行? Jquery 永远不会找到名为“ournewform”的元素,因此什么也不做!
  • 如果没有必要,为什么要使用 echo()?
  • 此方法无效。不过,谢谢你的建议。
  • 您的页面没有得到回显的脚本。
  • 对不起,Jai,我不知道那里发生了什么。但是,我确实尝试过,但它在这种情况下不起作用。
【解决方案2】:

你可以试试这样的。

$(document).ready(function() {
    $('#ournewform').fadeOut(2222);
    window.location ='newpage.html';
});

在您的 php 脚本中,您可以将其分配给一些变量,例如

<?php
$script = "$(document).ready(function() {
               $('#ournewform').fadeOut(2222);
               window.location ='newpage.html';
           });";

然后在你的&lt;body&gt;标签中,最好在页面底部,在关闭标签之前,你可以打印&lt;script&gt;标签里面的变量,比如:

<?php if(isset($script):?>
<script type='text/javascript'><?php print $script;?></script>
<?php endif;?>

将javascript放在页面底部将确保在表单加载后触发fadeOut事件。

您也可以使用setTimeOut 函数将fadeOut 延迟几毫秒。

【讨论】:

  • 对不起,Ehs4n。我试图在这里发布一些代码,但没有成功。我调整了您在此处放置的内容,但出现服务器错误:pastebin.com/8Bb0UWy2 您可以在 ournewlife.com/002/login.php 上看到它
  • $script = 应该放在你的 if 语句中,因为重定向只会在用户发布表单数据并经过验证后进行。还要评论header('Location...),因为它是在javascript代码本身中处理的,请参阅window.location
  • 我现在正在调查这个。我需要等一下我的服务器。感谢您抽出宝贵时间。
  • @Cheryl - 如果它解决了您的问题,请务必接受这个答案。
  • 它会重定向,但页面不会褪色。
【解决方案3】:

尝试将 Javascript 代码与 PHP 分开。如果先测试条件是否满足再注入javascript代码会更优雅:

<?php

session_start();

if ($_GET['login']) {
     // Only load the code below if the GET
     // variable 'login' is set. You will
     // set this when you submit the form

if (in_array($_POST['entryphrase'], array('enter', 'goin', 'entrezvous', 'opensesame'))) {
         // Load code below if both username
         // and password submitted are correct

         $_SESSION['loggedin'] = 1;
          // Set session variable
?>

<script type='text/javascript'>
      $('#ournewform').fadeOut(2222);      
</script>

<?php }
else echo "";
     // Otherwise, echo the error message
?>

不要忘记在标题部分包含 Jquery 库。

【讨论】:

  • 这个解决方案看起来不错,但是当我尝试时出现服务器错误:ournewlife.com/003/login.php
  • 这对我没有帮助。你能复制一下你遇到了什么错误吗?
  • 抱歉 esmimov,这是我遇到的唯一错误。也许你会对上面有一些见解......?该页面现在是半可操作的:ournewlife.com/005/login.php - 但是如果您使用“输入”登录,您会看到它闪烁,然后淡出并淡入(?!)......代码是在这里,这是迄今为止最好的工作解决方案,如果您对此有任何见解,那就太好了:pastebin.com/E0p1YQQS
  • @Cheryl 我想它按预期工作。我不再收到内部服务器错误,所以如果它解决了您的问题,请接受我的回答。
  • 正如我所说,该页面仍未按照预期的方式运行。如果您点击链接,您会发现它无法正常工作。
【解决方案4】:

您需要做的是:使用 ajax 函数并登录用户,根据响应,您可以选择淡出登录框或显示错误。

【讨论】:

  • Jaspal,我错过了你第一次发表的评论。你有什么资源建议可以提供一步一步的指导来完成这个吗?我还没有找到。如果您有任何也可能有帮助的搜索词,因为我不确定我是否在那儿打了它。谢谢!
【解决方案5】:

我在别处寻找这个问题的答案,但几乎没有找到任何帮助。但是,我确实在这里找到了登录脚本:

http://codecanyon.net/item/simple-secure-login-v3/2748367?sso?WT.ac=search_item&WT.seg_1=search_item&WT.z_author=phpcodemonkey

我仍然难以淡出,但似乎这种形式使用 ajax 等,因此它可能会有更多用处。我正在尝试在此处的另一个线程上收集解决方案:

fadeOut after ajax login submission

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-20
    相关资源
    最近更新 更多