PHP/Javascript 将消息传递到另一个页面

发布于 2024-08-21 21:58:28 字数 1093 浏览 4 评论 0原文

让我解释一下:

我基本上希望当您发表评论时(我使用 js/jquery 脚本将字符串发送到 insert.php 并插入数据库)您将收到 2+ 积分。现在我已经完成了,所以你得到+2分,但我想显示一条像stackoverflow这样的消息。我已经知道如何显示像 stackoverflow 这样的消息,但不知怎的,我需要从 insert.php(插入后)发送:

<div id='message' onclick="closeNotice()" style="display: none;">
Hey, <b><? echo $pusername; ?></b> - You've just got +<? echo $gpm; ?> points for your comment!
<a href="#" class="close-notify" onclick="closeNotice()">X</a>
</div>

到 index.php ..

我正在考虑可能编码到我当前的脚本中(正在发送字符串到 insert.php),它应该找到#message并将其扔进#box(div在index.php中称为“box”)。

但我该怎么做呢?我应该喜欢,在你完成 insert.php 之后,然后你激活 javascript 中的一个函数,它会执行以下操作:

function showmessage()  { 
    $("#box").html(data).find("#message").fadeIn("slow")
}

正如我所说,你激活脚本执行以下操作:

<script type="text/javascript" language="javascript">
showmessage();
</script>

在你成功插入数据库并向用户提供积分之后? 我刚刚测试过这个,但无法让它工作。 我的网站与 phpBB 登录的会话集成(我有 phpBB 论坛),所以我认为我不能使用 $_SESSION。 insert.php 在框架中打开。 我的问题是操作和确认显示发生在不同的页面上。

So let me explain:

I basically want so when you post a comment, (i use a js/jquery script to send string to insert.php which inserts to the database) you will receive 2+ points. Now i have done so you get +2 points, BUT i want to display a message like stackoverflow. I already know how to display a message like stackoverflow, but in somehow i need to send from insert.php(after you inserted), this:

<div id='message' onclick="closeNotice()" style="display: none;">
Hey, <b><? echo $pusername; ?></b> - You've just got +<? echo $gpm; ?> points for your comment!
<a href="#" class="close-notify" onclick="closeNotice()">X</a>
</div>

to index.php..

I was thinking of maybe coding into my current script(that are sending string to insert.php) that it should find #message and throw it in #box (div called "box" in index.php).

But how should i do this? Should i like, after you got through insert.php, then you activate a function in javascript that does:

function showmessage()  { 
    $("#box").html(data).find("#message").fadeIn("slow")
}

and as i said you activate the script doing:

<script type="text/javascript" language="javascript">
showmessage();
</script>

after you succesfully have inserted to database and gived points to the user?
Ive just tested this, and i cant get it to work.
my site is integrated with sessions from the phpBB login (phpBB forum i've got), so i don't think i can use $_SESSION.
And the insert.php is opened in a frame.
My problem is that the action, and the displaying of the confirmation take place on different pages.

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

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

发布评论

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

评论(3

甜中书 2024-08-28 21:58:28

如果我理解正确的话,你的问题是操作和确认的显示发生在不同的页面上。

执行此操作的一种方法是存储要在用户会话中的下一页上显示的消息:

// insert.php
$_SESSION["user_message"] = "You were awarded +2 points.";

并将其输出到下一页上:

// thankyou.php
echo $_SESSION["user_message"]; // Or show the box, or whatever
$_SESSION["user_message"] = null; // Clean up

这样做的潜在缺点是,如果用户有两个或更多页面/选项卡如果您的网站打开,并在其中进行多次导航,则该消息可能会出现在错误的上下文中。例如,如果我在选项卡 A 中单击“保存”,然后刷新选项卡 B,则可能会出现用于选项卡 A 的消息显示在选项卡 B 中的情况。

您可以通过向消息的变量名称添加随机生成的键来帮助实现这一点,并将该密钥传递到您想要显示消息的页面:

// insert.php
$key = "123456"; // Insert random generation method here, e.g. using rand()
$_SESSION["user_message_$key"] = "You were awarded +2 points.";
header ("Location: thankyou.php?message=$key"); // Pass the key to the next page

// thankyou.php
$key = $_GET["message"]; // No sanitation necessary here AFAICS
echo $_SESSION["user_message_$key"]; // Or show the box, or whatever
$_SESSION["user_message_$key"] = null; // Clean up

这非常优雅,因为

  • 您想要显示的消息保留在内部会话存储中,并且不会在浏览器中传递,从而减少了安全漏洞等风险。

  • 通过取消设置会话变量,您可以确保消息仅显示一次,即使用户重新加载页面也是如此。

If I understand you correctly, your problem is that the action, and the displaying of the confirmation take place on different pages.

One approach to do this is to store the message that is to be displayed on the next page in the user's session:

// insert.php
$_SESSION["user_message"] = "You were awarded +2 points.";

and output it on the following page:

// thankyou.php
echo $_SESSION["user_message"]; // Or show the box, or whatever
$_SESSION["user_message"] = null; // Clean up

the potential downside to this is that if the user has two or more pages/tabs of your site open, and navigates a lot across them, the message may appear in the wrong context. For example, if I click "save" in tab A, and refresh tab B, it could happen that the message intended for tab A is displayed in tab B.

You could help that by adding a randomly generated key to the message's variable name, and passing that key on to the page you want to display the message on:

// insert.php
$key = "123456"; // Insert random generation method here, e.g. using rand()
$_SESSION["user_message_$key"] = "You were awarded +2 points.";
header ("Location: thankyou.php?message=$key"); // Pass the key to the next page

// thankyou.php
$key = $_GET["message"]; // No sanitation necessary here AFAICS
echo $_SESSION["user_message_$key"]; // Or show the box, or whatever
$_SESSION["user_message_$key"] = null; // Clean up

This is very elegant because

  • the message you want to display remains in your internal session store, and at no point is passed on in the browser, reducing the risk of security holes and such.

  • by unsetting the session variable, you make sure the message is shown only once, even if the user reloads the page.

不弃不离 2024-08-28 21:58:28

如果需要显示消息的页面正在打开insert.php,则可以使用window.opener 访问原始页面。我认为这样的事情可能会起作用:

window.opener.$("#message").html($("#box").html()).fadeIn("慢");

If insert.php is being opened by the page that needs to display the mesage, you can use window.opener to access the originating page. I think something like this might work:

window.opener.$("#message").html($("#box").html()).fadeIn("slow");

孤独难免 2024-08-28 21:58:28

我不确定我是否理解正确,但是如果用户在index.php上的字段中进行输入,那么也许您可以使用ajax请求将其发送到insert.php,并使用ajax的回调来显示通知用户?

I'm not sure if I understand correctly, but if the user makes the input in a field on index.php, then maybe you could send it to insert.php with an ajax request, and use the callback of the ajax to display the notice to the user?

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