简体   繁体   English

PHP:在指定的时间内回显消息

[英]PHP : echo message for a specified amount of time

I want to display a string "Your status has been posted" for something around 3 or so seconds than I want it to go away. 我想显示一个字符串“您的状态已发布”,持续约3秒钟,而不是我希望它消失的时间。

As of right now I have a news feed where the user can post messages and after they post it echoes that string of text until the url is re-enetered. 截至目前,我有一个新闻提要,用户可以在其中发布消息,并在它们发布后回显该文本字符串,直到重新输入URL。 Does anyone have any suggestions? 有没有人有什么建议?

if ($_POST['submit'])
{
$body = $_POST['body'];

if ($body)
{
    include ('connect.php');
    $date = date("Y-m-d");
    $email = $_SESSION['email'];
    $who = mysql_query("SELECT firstname FROM people WHERE email = $email");
    $insert = mysql_query("
    INSERT INTO status VALUES('','$email','$body','$date')");
    echo ('Your status has been posted <p></p>');
}
else
    echo 'Status required to post into news feed! <p></p>';
 }
 ?>

Status : 状态 :

Thanks for your help! 谢谢你的帮助!

Jeff 杰夫

You need to add some JavaScript to do this. 您需要添加一些JavaScript来执行此操作。 Here is an outline to get you started: 以下是入门指南:

<p id="info-message">Your status has been posted</p>
<script>
  setTimeout(function(){
    document.getElementById('info-message').style.display = 'none';
    /* or
    var item = document.getElementById('info-message')
    item.parentNode.removeChild(item); 
    */
  }, 3000);
</script>

Also you may want fade-out effects. 另外,您可能需要淡出效果。 For that I'd recommend using a library like jQuery or Scriptaculous. 为此,我建议使用jQuery或Scriptaculous之类的库。

Useful links: 有用的链接:
https://developer.mozilla.org/en/Window.setTimeout https://developer.mozilla.org/zh-CN/Window.setTimeout
http://api.jquery.com/fadeOut/ http://api.jquery.com/fadeOut/
http://script.aculo.us/ http://script.aculo.us/


Oh, by the way, your script is vulnerable to SQL injection . 哦,顺便说一句, 您的脚本容易受到SQL注入的攻击。 Always escape your query parameters (or use prepared queries)! 始终转义查询参数(或使用准备好的查询)!

You can't do that with php. 您无法使用php做到这一点。 Try Javascript instead. 请尝试使用Javascript。

Unless you go for some weird "100 Continue" approach interleaved before you present your next web page you will need to do this in javascript. 除非您在提出下一个网页之前采用了一些奇怪的“ 100继续”方法,否则您将需要使用javascript。

If this i to display for a period after they first see a webpage then you can have something like a This message will disappear and then add a script which is called from your tag's onLoad action which would (eg) sleep for three seconds and then set the content of that div to be empty. 如果此消息在他们第一次看到网页后显示一段时间,则您可以看到类似“此消息”的信息,然后添加一个脚本,该脚本从标签的onLoad操作中调用,该脚本将(例如)休眠三秒钟,然后进行设置该div的内容为空。

I'd use AJAX for that. 我会为此使用AJAX。 You can assure yourself that the data has been well inserted into the DB, and then show the message with javascript for amount the time you want. 您可以确保自己已将数据正确插入数据库中,然后使用javascript显示消息,显示所需的时间。

If sending output to the browser, then you can use javascript to control the time-window you want your text to be visible. 如果将输出发送到浏览器,则可以使用javascript来控制您希望文本可见的时间窗口。

If using jquery you could use .hide() http://api.jquery.com/hide/ 如果使用jquery,则可以使用.hide() http://api.jquery.com/hide/

You cannot achieve this with PHP only; 您不能仅使用PHP来实现; you need JavaScript. 您需要JavaScript。 Here's a simple example using jQuery: http://jsfiddle.net/4surX/ 这是一个使用jQuery的简单示例: http : //jsfiddle.net/4surX/

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

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