tags:

views:

73

answers:

3

Hi,

For some reason, my jQuery isn't working properly, and I can't spot the mistake, although I may have some idea. Below is some of the code from group2.php, its for a chat application:

<div id="wrapper">
    <div id="menu">
        <p class="welcome">Welcome, <b><?php echo $_SESSION['name']; ?></b></p>
        <p class="logout"><a id="exit" href="?logout=1">Exit Group</a></p>
        <div style="clear:both"></div>
    </div>  

   if(isset($_GET['logout'])){  
     $fp = fopen("log.html", 'w');
     fwrite($fp, "");
     fwrite($fp, "<div class='msgln'><i>User " . $_SESSION['name'] . " has left the chat session.</i><br></div>");
     fclose($fp);       
     session_destroy();
     header("Location: main.php"); //Redirect the user        
     }



?>
</div>

While below is jQuery, which is also in group2.php, where I am trying to see if the user clicked logout, then we should direct to main.php?logout=true

//If user wants to end session
    $("#exit").click(function(){
      var exit = confirm("Are you sure you want to end the session?");
      if(exit==true){ window.location = 'main.php?logout=true';
      }
    }); 

While I am able to execute the code within if(isset($_GET['logout'])), and ask the user whether they would like to leave the room, I am unable to direct the user to main.php?logout=true after they click 'ok' to the question 'Want to end session?'. Instead, the user is being directed to ?logout=1. Any suggestions on what I'm doing wrong? Thank you.

+1  A: 

You need to prevent the default action in this case, which is to go to the href in the <a>. You can do that with event.preventDefault() or return false, like this:

$("#exit").click(function(e) {
   if(confirm("Are you sure you want to end the session?")) {
     window.location = 'main.php?logout=true';
   }
   e.preventDefault();
   //or: return false;
}); 

Or, if you're never going to use it, just remove the href url, like this:

<a id="exit" href="#">Exit Group</a>
Nick Craver
Nick, after following your steps I am being directed to main.php?logout=true, as I had wished but now my code for if(isset($_GET['logout'])) won't execute. i.e. the file won't truncate. When I used your latter advice, the same thing happened. Any suggestions?
Newbie_25
@newbie-25 - Is `main.php` the page you posted? I thought from the question it's `group2.php`, what does `main.php` look like?
Nick Craver
@Nick Craver - Right, both of the above codes come from group2.php, where the chat application is located. But when the user clicks 'Exit Group', I want him/her to be directed to main.php or main.php?logout=true. Main.php is where OTHER chat rooms are located, incase the user wants to enter other rooms. Am I doing something wrong here?
Newbie_25
+4  A: 

Because true is just a constant for 1

Also your header() call in the PHP to redirect won't work because you've output HTML already.

Cfreak
+1  A: 
Kelly Copley
@ Kel - Thank you for your former part. Everything as of now is working fine for the chat application, except I was unable to keep the user on the chat page just in case if they had clicked 'cancel' just like you mentioned. But, when I applied your code, the isset($_GET['logout'] code would NOT work. I.e. the file won't truncate (That is exactly what I want it to do because when a user leaves a chat session, I want their old messages to be deleted on THEIR screen at least. Therefore, preventDefault seems to make the javascript work but it also causes my other code to not execute. Hmm.
Newbie_25
I'm sorry for the confusion, I should have specified that your url in the href needs to contain the link with the parameters..<a href="main.php?logout=true">Logout</a>then when they click ok, the default will not be prevented and their browser will follow the link and log out.
Kelly Copley