0

I have PHP code like this: (for signing out - located in a php page together with other php handlers for different functions and this file is included in pages like index.php or other relevant pages.)

if(isset($_POST['signout'])) { // logout button
  // Clear and destroy sessions and redirect user to home page url.
  $_SESSION = array();
  session_destroy();
  // redirect to homepage (eg: localhost)
  header('Location: http://localhost/index.php');
  }

I normally use a <form action="index.php" method="post"> where the function is currently included and <input type="submit" name="signout"> for such things but this time i would like to use an anchor tag like: <a href="" >Sign Out</a> for signing out.

Would anyone be kind enough to show an example code that will trigger a submit that will be handled by the given PHP code above.

Either a jQuery or Javascript solution would do. I would just like to see a complete example on how this works.

5
  • 1
    Just get rid of the $_POST['signout'] requirement and throw this in something like logout.php that you can link to. Commented May 28, 2013 at 21:17
  • Hi, the $_POST['signout'] is located in a php file with other functions and this file is currently included in the relevant pages like index.php or client_panel.php - I am currently wondering how can i use the anchor tag to be submit a value to be handled by this function as I have designed and located the Signout on a sidebar of <li> that has other links to pages like <a href="update_profile.php">Update Profile</a>. Commented May 28, 2013 at 21:25
  • Well, you could display your form submit button as a link or use JavaScript to send a POST. Or, you could modify your existing code. Commented May 28, 2013 at 21:26
  • Since sign out is an action, you should really use a POST request. You should be able to style a <button type="submit"> like an anchor tag if you want to. Commented May 28, 2013 at 21:28
  • @Brad Yes but I don't know how to achieve that as I just started developing and completely new to JS/jQuery - that's why I wanted an example on how to achieve this so that I can somehow understand and use it in the future. Commented May 28, 2013 at 21:30

1 Answer 1

1

There's good reason for using a POST for submitting authentication tokens which usually commences or alters the session state - but these don't really apply to the problem of closing the session - why not just just trigger this via a GET?

But if you really must do a POST, and you really must do it via a a href (rather than styling a submit button) and you really must do it via javascript (which will break if the client has javascript disabled) then...

<script>
function sendForm(formId) 
{
   if (document.getElementById(formId).onsubmit()) 
      document.getElementById(formId).submit();
}
<script>
<form id='logout'>
<input type='hidden' name='signout' value='1'>
</form>
<a href="javascript:sendForm('logout');">logout</a>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank You @symcbean . By the way, I just tried this code but it seems to break the rest of the page when the code <a href="javascript:sendForm('logout');">logout</a> is included. This also happens when I try to use a onclick when the a anchor is inside a <li> . Also, how do you suggest I achieve this via the GET method. I have already tried using the CSS trick for making a submit look like a link. I am just looking for different ways to achieve this especially that I see most of the codes I try to study use this method of using an anchor as submit. Apologies as I am still learning.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.