Newbie_25
Newbie_25

Reputation: 835

Jquery not working

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.

Upvotes: -1

Views: 247

Answers (3)

Kelly Copley
Kelly Copley

Reputation: 3158

First lets start with your javascript:

Personally I would cause it to confirm and then prevent the default if they click the cancel button like this.

$("#exit").click(function(e) {
   if(!confirm("Are you sure you want to end the session?")) {
     e.preventDefault();
   }
});

This would cause the link not to be followed if they click cancel but to follow the link if not. The reason I would do it this way is because if they have javascript turned off they won't get the confirmation dialog but the logout will still work as intended.

Next your PHP/HTML:

if you are using the exact code you posted then you missed a

<?php
 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
    exit;

 }
?>

If your header functions are not executed before ANY output, even a single blank space it, will cause an error. The exception to this is if output buffering is turned on in your php.ini file. The problem with that is if you want to put the a diff server that doesn't have this turned on it will err unexpectedly. Alternatively you could use ob_start() and ob_end_flush() at the beginning of ever script but truly output buffering is a crutch that shouldn't be relied on when its just as easy to structure your code so that you don't need it.

Other things I noticed is that you are using a text file. I would definitely consider a database. If you do insist on text file, you might want to change the 'w' in your fopen to an 'a' so you are appending the file instead of writing. The w option will truncate the file each time you run this script so you will start with an empty file then write your line and the next time the script is executed you will get an empty file.

Hope this helps

-kel

Upvotes: 1

Cfreak
Cfreak

Reputation: 19309

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.

Upvotes: 3

Nick Craver
Nick Craver

Reputation: 630379

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>

Upvotes: 1

Related Questions