views:

341

answers:

3

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

+5  A: 

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.

Pekka
Hi. This method seems..alright, i will test it now, but i got one question. I want this to display in index.php, when you clicked insert.. I understanded that i just can make a section in index.php that takes messages and show them. But after insert i dont want it to header location to index.php. So how should i pass it, another method. e.g javascript, no?
Karem
And another reason the header location can't work with it, is that the insert.php is working inside a frame, and the index.php is outside(design and all). do you feel me? So it would display index.php inside the frame..
Karem
Ops, forgot to mention, my site is integrated with sessions from the phpBB login (phpBB forum i've got), so i don't think i can use $_SESSION. But i gave it a shot, and tested it but im just getting out undefined variable _SESSION. Using $_GET method works though, but still i have the problem to pass it..(header wont work properly for my case) Hope you can help me out Pekka
Karem
Rails calls this concept "Flash" data - special session data that persists only until the next page is requested, then it is discarded. It's reasonably trivial to implement `flash_set($message)` and `flash_get()` methods in PHP, and they can be extremely useful.
meagar
@Azzyh You should update your question to reflect all these restrictions- phpBB, insert.php being open in a frame, and your unwillingness to reload index.php to display the notice. These restrictions imply a drastically different solution.
meagar
Hi meagar- So an idea: by setting flash_set in insert.php, and then make a refresh script that refreshes "messages.php" each 2 seconds, which contains flash_get, in index.php, will that work?
Karem
@meagar feel free to add an answer of your own, you can discuss it there (too much confusion otherwise) :)
Pekka
Is this impossible to do?
Karem
@Azzyh not at all, but as meagar says, a fundamentally different scenario than what I outline in my answer.
Pekka
Okay, i'll wait for meagar´s response/answer
Karem
A: 

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

nafisto
A: 

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?

Adrian Schmidt