www.webdeveloper.com
Results 1 to 7 of 7

Thread: Flagging

  1. #1
    Join Date
    Aug 2009
    Posts
    53

    Angry Flagging

    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>
    the variables like $r['xxxxx'] get info from a MySQL database.

  2. #2
    Join Date
    Jan 2005
    Posts
    362
    I don't think I understand what you mean by 'flagging'

  3. #3
    Join Date
    Aug 2007
    Posts
    3,767
    Refresh the page? Cookies or GET variables.
    Great wit and madness are near allied, and fine a line their bounds divide.

  4. #4
    Join Date
    Aug 2009
    Posts
    53
    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;\">";
    (the $r['id'] gets the ID of the MySQL row)

    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>";
    the background image of that specific DIV changes (see javascript code).

    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. #5
    Join Date
    Jan 2005
    Posts
    362
    Declan has it 100&#37; 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. #6
    Join Date
    Aug 2009
    Posts
    53
    How would I do that?

    I can set cookies using:
    Code:
    document.cookie = 'flag'+divid+'=true; expires=; path=/';
    However, I don't know how to delete cookies or how to read all the "flagxx" cookies that would have been set.

  7. #7
    Join Date
    Jan 2005
    Posts
    362
    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.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles