开发者

PHP/Javascript passing message to another page

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 k开发者_StackOverflow中文版now 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.


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.


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


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜