|
|||||||
| JavaScript JavaScript (not Java) Discussion and technical support, including AJAX and frameworks (JQuery, MooTools, Prototype...) |
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
I am making a web-app that has flagging in it. I've got that working, however, it doesn't maintain through a refresh (which happens every 20 seconds or so). How do I make it maintain through a refresh.
Here's a stripped-down version of my code: HTML Code:
<html> <head> <script type="text/JavaScript"> function load() { window.setTimeout('window.location="display.php"; ',<?php echo $u['refresh']; ?>+"000"); } function flag(id,btn) { var divid = id; var abtn = btn; document.getElementById(divid).style.background="#F0D7D7 url('display-bg-flagged.png') repeat-x"; document.getElementById(btn).href="javascript:unflag('"+divid+"','"+abtn+"')"; document.cookie = 'flag'+divid+'=true; expires=; path=/'; } function unflag(id,btn) { var divid = id; var abtn = btn; document.getElementById(divid).style.background="#F0F0F0 url('display-bg.png') repeat-x"; document.getElementById(btn).href="javascript:flag('"+divid+"','"+abtn+"')"; createCookie("flag"+id,"",-1); } </script> </head> <body onLoad='load()'> <?php $user = $_COOKIE["logged-in"]; $posts = $u['posts']; $result = mysql_query("select * from visitordata where user='".$_COOKIE['logged-in']."' order by id desc limit $posts"); //the while loop $id=$_GET['id']; echo "<div id=\"feedback\">"; while($r=mysql_fetch_array($result)) { echo "<div class=\"post\" id=\"".$r['id']."\" name=\"flag\" style=\"background:#F0F0F0 url(display-bg-flagged.png) repeat-x;\">"; echo "<div class=\"info\"><a href=\"delete.php?delid=".$r['id']."\" title=\"Delete Feedback\"><img src=\"delete.png\" border=\"0\"></a> "; echo "<a href=\"javascript:flag('".$r['id']."','flagbtn".$r['id']."');\" title=\"Flag Feedback\" id=\"flagbtn".$r['id']."\"><img src=\"flag.png\" border=\"0\"></a>"; echo " | <div class=\"time\">".$r['time']."</div> | <div class=\"name\">".$r['name']."</div> | <div class=\"location\">".$r['location']."</div> | <a href=\"http://".$r['ip_address']."\">".$r['ip_address']."</a></div>"; echo "<a href=\"mail.php?mailid=".$r['id']."\" title=\"Email Feedback\"><img src=\"email.png\" border=\"0\"></a> <a href=\"block.php?ip=".$r['ip_address']."&user=".$_COOKIE['logged-in']."\"><img src=\"block.png\" border=0></a> | <div class=\"message\">".$r["message"]."</div></div></div>"; } echo "</div>"; ?> </body> </html> |
|
#2
|
|||
|
|||
|
I don't think I understand what you mean by 'flagging'
|
|
#3
|
|||
|
|||
|
Refresh the page? Cookies or GET variables.
__________________
If I have helped Quote:
|
|
#4
|
|||
|
|||
|
It's kinda hard to explain it, but I'll try my best.
The PHP while continually loops until all the relevant data has been collected from a MySQL database. Each time it loops, it creates a DIV: Code:
echo "<div class=\"post\" id=\"".$r['id']."\" name=\"flag\" style=\"background:#F0F0F0 url(display-bg.png) repeat-x;\">"; When I press the flag button: Code:
echo "<a href=\"javascript:flag('".$r['id']."','flagbtn".$r['id']."');\" title=\"Flag Feedback\" id=\"flagbtn".$r['id']."\"><img src=\"flag.png\" border=\"0\"></a>";
My question is: when I 'flag' a DIV, the background changes, but when the page automatically refreshes, it returns to the original color. But I want it to STAY on the new color. |
|
#5
|
|||
|
|||
|
Declan has it 100% correct:
either set a cookie when the flag button is pressed, and look for that same cookie every time the page is loaded, or else set a GET parameter, like when posting a form, and check for _that_ every time the page loads. I note that you are already setting cookies, so I presume you are aware that they are unreliable? |
|
#6
|
|||
|
|||
|
How would I do that?
I can set cookies using: Code:
document.cookie = 'flag'+divid+'=true; expires=; path=/'; |
|
#7
|
|||
|
|||
|
You would not need to delete a cookie if you did: flag= divid
Just set it to null if nothing is selected. Reading cookies is pretty easy, and there are plenty of tutorials online, but a little too much for me to go into now. |
![]() |
| Bookmarks |
| Tags |
| flag, maintain, php, refresh, variable |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|