0

Hello need help to run the following php in a html.

function runMyFunction(){
      $_SESSION["identificate"] = 'no';
      session_destroy();
      header('Location: Login.html');
}
---------------------------------------------------------
 <div id="foo">
    <button type="button" onclick="runMyFunction()">Logout

    </button>
 </div>

Thanks

Polo D. Vargas
  • 1,649
  • 2
  • 14
  • 23
  • PHP is a serverside language. This means it's performed before the visitor sees the output in the browser. The best solution would be to create a redirect with Javascript towards the php file and use `header('location: .. ')` to send them back again or use Ajax. – icecub Nov 28 '15 at 17:14

1 Answers1

2

You should execute an ajax call with the help of jQuery.
We can use the shorthand method .post to issue a post request directly.

First put the php code in a separate file, called logout.php

logout.php

function runMyFunction(){
      $_SESSION["identificate"] = 'no';
      session_destroy();

      //header('Location: Login.html'); we will move the redirect to the success handler of the ajax call
}

//call the function
runMyFunction();

Then in the html, include jquery and add a script block like below

<script type="text/javascript">
$(function() {
    $("#foo button").click(function(){
        $.post( "logout.php", function( data ) {
            console.log("success");
            window.location = "login.html"; // redirect moved here, after the logout happened successfully
        });
    })
});
</script>

You can remove the onclick attribute from the button, so your button html will become this

<button type="button">Logout</button>

This is how you include jQuery

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
Alex Andrei
  • 7,315
  • 3
  • 28
  • 42
  • If you're going to use `$("#foo button")` then the button requires an id. – Funk Forty Niner Nov 28 '15 at 17:22
  • `foo` is the id of the div containing the button, button doesn't need id. Unless I'm missing something. – Alex Andrei Nov 28 '15 at 17:23
  • ah, you're probably right Alex. My mistake ;-) – Funk Forty Niner Nov 28 '15 at 17:24
  • edited the answer to move the redirect in the ajax success callback – Alex Andrei Nov 28 '15 at 17:27
  • Im sorry i am working with php and html so i don't know much about js and jquery. That script you made will modify like what my button 'foo' will do? where in that script do i write my "runMyfunction()" so that when i click the button it will execute it? – Polo D. Vargas Nov 28 '15 at 17:32
  • I'm not sure I understand your last question. What happens, in simple terms, is that when you click on the button, you execute the php function, in the background :) – Alex Andrei Nov 28 '15 at 17:35
  • what i mean is that i don't see in the script the name of my function called "runMyfuntion()", so i was wondering if i should replace it somewhere. Should i replace "logout.php" for runmyfunction? – Polo D. Vargas Nov 28 '15 at 17:40
  • the function is in the separate php file, the function is called inside the php file when it is executed via the `.post` request. Read the answer and the comments inside – Alex Andrei Nov 28 '15 at 17:43
  • Thanks you ! it worked – Polo D. Vargas Nov 28 '15 at 17:51
  • Sorry for bothering you again but the function in the php file is not running it is just like the one your wrote. The script is working tho because the page is redirected. What could be going wrong? – Polo D. Vargas Nov 28 '15 at 19:15
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/96437/discussion-between-alex-andrei-and-polo-d-vargas). – Alex Andrei Nov 28 '15 at 19:18