Jump to content

onclick functions.


Foser

Recommended Posts

I'm not sure if this exists, because I haven't heard of anything like this.

 

But I'm trying to do  logout function with session and want to do something like.

 

The only way I would think of doing it. would click send to a different page something like.

 

<?php

session_destroy();

if ($_SESSION['LOGGEDIN'] == FALSE){
header(Location: /login.php); exit;}

else { 
header(Location: /account.php);}

?>

 

is there a way so onclick of (this) will do {this function}?

 

or my way is the only way?

Link to comment
Share on other sites

yes here is how:

 

<?php
function logout() {
session_destroy();

if ($_SESSION['LOGGEDIN'] == FALSE){
header('Location: /login.php'); exit;}

else { 
header('Location: /account.php');
}
}
?>
<html>
//This is your form
<form id="form1" name="form1" method="post" action="">
  <label></label>
  <input type="submit" name="logout" id="logout" value="Logout" onClick="logout()" />
</form>
</html>

 

Please note that when you call the "HEADER" command it is always followed by (' and closed by '); so u would change

 header(Location: /login.php);

to

header('Location: /login.php');

Link to comment
Share on other sites

Do this:

<?php

if ($_POST['submit']){

   session_destroy();

   if (!isset($_SESSION['LOGGEDIN'])){
      header('Location: /login.php'); 
   } else { 
      header('Location: /account.php');
   }
}


?>

<form method="post" action="<?=$_SERVER['PHP_SELF'] ?>">
  <input type="submit" name="logout" id="logout" value="Logout">
</form>

Link to comment
Share on other sites

If you want to test for FALSE, I'm pretty sure you have to use ===.

 

Besides the fact that that is incorrect.. whats does it have to do with the op's question here?

 

WOO! I smell smoke. charlieholder was just trying to point out a flaw in their code....or trying to at least. No harm in that.

Link to comment
Share on other sites

If you destory the session with session_destroy(), can you still store in the global variable $_SESSION?

 

0_0 Are you sure you just posted on the right thread? ...if you have a question totally unrelated to the topic, you should probably post your own thread for it.

Link to comment
Share on other sites

bigbob - Are you sure you can mix JavaScript with PHP like that? I'm thinking you can't....maybe I'm wrong though.

 

If that doesn't work, You could use a submit button and it won't require the onclick function.

 

 

Im pretty sure that that will work. Try it though.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.