Run PHP code within a jQuery condition

Asked

Viewed 570 times

0

I have a modal window that opens, shows the option to click another button, that says yes or no.

If I click yes, I intend to destroy the PHP session.

How can I do this within the jQuery code that opens the modal window?

JQUERY:

$(document).ready(function(){
     $('#modalLogout').click(function(){
          $('#yes').click(function(){
               // preciso de destruir a sessão <?php session_destroy(); ?>
          });
     });
});

I tried to use:

$(this).load("destroy.php");

And send to a PHP page but it didn’t work!

1 answer

1


You can redirect to your page destroy.php that is probably destroying the session and making another redirect to the login screen.

$(document).ready(function(){
     $('#modalLogout').click(function(){
          $('#yes').click(function(){
              window.location = "destroy.php"

If you are going to use Ajax, you need to recover the destroy.php need to return something for Javascript to know the current state of the page and do what is necessary, show a message or redirect the user somewhere.

  • Here it didn’t work, it redirects to Destroy.php but when I return to the page the session is still active... I also noticed that if I insert <?php session_destroy(); ? > inside the link of the modal button it destroys the session, but that can not be because it is in a place where I can not do it, because it will implicate me in another part of the code, so I wanted inside the jquery.

  • So you are not destroying the session, you can share that file?

  • <?php session_destroy(); header("Location: ../index.php"); ?>

  • 1

    And how you redirected to this page in your Javascript?

  • I followed your example above adding the window.Location = "Destroy.php" to my jquery code

  • And you were redirected? You even uploaded the file destroy.php? You don’t need to put the session_start() before calling the session_destroy()?

  • I solved the problem with your example, the PHP session was not properly set, so the Destroy.php page did not work!

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.