PHP/Javascript将消息传递到另一个页面 [英] PHP/Javascript passing message to another page

查看:104
本文介绍了PHP/Javascript将消息传递到另一个页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以让我解释一下:

我基本上想要这样,当您发表评论时(我使用js/jquery脚本将字符串发送到insert.php并将其插入到数据库中),您将获得2分以上.现在我已经完成了,所以您得到+2分,但是我想显示类似stackoverflow的消息.我已经知道如何显示类似stackoverflow的消息,但是以某种方式我需要从insert.php发送(插入后),这是

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>

到index.php ..

to index.php..

我当时正在考虑可能要对我当前的脚本(将字符串发送到insert.php中)进行编码,以使其应该找到#message并将其扔到#box(在index.php中称为"box"的div)中.

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).

但是我应该怎么做?在插入insert.php之后,我是否应该在javascript中激活一个执行以下操作的函数:

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>

成功插入数据库并为用户指定分数后? 香港专业教育学院刚刚测试了这一点,我不能让它工作. 我的网站与phpBB登录(我拥有的phpBB论坛)的会话集成在一起,所以我认为我不能使用$ _SESSION. 然后insert.php在一个框架中打开. 我的问题是操作和确认的显示在不同的页面上进行.

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.

推荐答案

如果我对您的理解正确,那么您的问题是操作和确认显示在不同的页面上.

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.";

并将其输出到下一页:

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

这样做的潜在弊端是,如果用户打开了网站的两个或多个页面/选项卡,并在它们之间进行了很多浏览,则该消息可能会在错误的上下文中显示.例如,如果我单击选项卡A中的保存",然后刷新选项卡B,则可能会在选项卡B中显示用于选项卡A的消息.

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

这很优雅,因为

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

  • 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.

这篇关于PHP/Javascript将消息传递到另一个页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆