如果 isset..php/javascript 则执行操作

发布于 2024-08-22 02:29:18 字数 302 浏览 8 评论 0原文

插入评论时我正在存储一个会话变量..称为 user_message.. 我想,当 user_message 已存储时,然后在 index.php 上显示消息 index.php 现在是:

<div id="message">
<? echo $_SESSION["user_message"]; ?>
</div>

但是您需要刷新(f5)站点,才能在存储后查看消息..

但我想如果变量 user_message 已存储某些内容,则显示它..

希望您理解。

I am storing a session variable when a comment is inserted.. called user_message..
and i want to, when user_message, have been stored, THEN, show message on index.php
index.php is right now:

<div id="message">
<? echo $_SESSION["user_message"]; ?>
</div>

But you will need to refresh(f5) the site, to see the message AFTER its stored..

But i want to if variable user_message has got stored something, then displays it..

Hope you understand.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏九 2024-08-29 02:29:18

我不确定我是否理解您的意图 - 一旦用户发送了新评论,您是否想要刷新用户所在的页面?

如果是这样 - 如果您使用的是普通形式的 post/get,那么您可以使用上面编写的代码 @Seb 将用户请求重定向到您想要的页面,并显示通知。

如果您使用 ajax 发送评论,则可以从 ajax 调用返回响应,通知创建页面需要刷新的 ajax 调用的 javascript 函数,然后将 location.href 变量设置为您设置的 URL想。

但是 - 如果您想刷新所有用户看到评论页面的页面,一旦另一个用户发送了评论,您将需要每隔几秒/分钟重复进行 ajax 调用,以检查服务器中是否有某些内容发生更改,如果是这样,请使用 location.href 变量将用户重定向到不同的位置。
如果您仅依赖评论者计算机上保存的会话变量,则无法执行此操作,因为只有当原始评论者使用您的网站时才能访问该变量。

I'm not sure I understand your intention - do you want to refresh the page the user is on, once he/she has sent a new comment ?

If so - if you're using a normal form post/get, then you can redirect the user request to which ever page you want, and show a notice, using the code @Seb wrote above.

If you're using ajax to send the comment, you can return a response from the ajax call, notifying the javascript function that created the ajax call that the page needs to refresh, and then set the location.href variable to what ever url you want.

However - if you want to refresh the page for all users seeing the comments page, once another user has sent a comment, you will need to have a repeated ajax call every few seconds/minutes, that checks if something has changed in the server, and if so use the location.href variable to redirect the user to a different.
You can't do this if you're relying only on the session variable saved on the computer of the commenter, as it is only accessible when the original commenter is using your site.

兔姬 2024-08-29 02:29:18
<?php if( isset($_SESSION["user_message"]) ): ?>
  <div id="message">
    <? echo $_SESSION["user_message"]; ?>
  </div>
<?php endif; ?>
<?php if( isset($_SESSION["user_message"]) ): ?>
  <div id="message">
    <? echo $_SESSION["user_message"]; ?>
  </div>
<?php endif; ?>
所有深爱都是秘密 2024-08-29 02:29:18

如果您希望消息与评论表单显示在同一页面上,我推荐一些 JavaScript。如果您使用 jQuery http://jquery.com/ 那么您可以执行类似的操作。

$('form#CommentFormID').submit(function() {
    var message = $('form#CommentFormID textarea#comment').html();
    $('body').append('<div id="message">' + message + '</div');
});

没有尝试过这个,但应该可以正常工作。

I recommend some Javascript if you want the message to be displayed on the same page as the comments form. If you're using jQuery http://jquery.com/ then you can do something like.

$('form#CommentFormID').submit(function() {
    var message = $('form#CommentFormID textarea#comment').html();
    $('body').append('<div id="message">' + message + '</div');
});

Haven't tried this, but it should work fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文