0

i have a functions.php page with a dologout function

function doLogout() {
    if (isset($_SESSION['user_id'])) {
        unset($_SESSION['user_id']);
        unset($_SESSION['location']);
    }
    header('Location: index.php');
    exit;
}

and a home.php page with a logout button with onclick method to logout function which is as follow

function logout()
                            { 
                              $.ajax({
                              url: "functions.php",
                              type: "POST",
                              dataType: "dataString",
                              data: {func:"doLogout"},
                              }).done(function() {
                              alert( "LoggedOut Succesfully");
                               }); 
                                }
                              }
                       }

But this function is not working i tried looking at other answers but none of them actually answers how to call a function using ajax. Can anyone please help me? Thanks in advance

  • 1
    are you expecting that `func:"doLogout"` calls `doLogout()` by himself? – Federkun Sep 07 '17 at 22:23
  • yes i searched for other answers online and one of it said to try it this way so i did but teven this isnt working – Mohammed Sayer Sep 07 '17 at 22:25
  • if i use a $_POST method in functions.php then it gives an error as that gets post only when logout button is been hit on the home screen in other cases it thorws an error saying $_post method variable is undefined – Mohammed Sayer Sep 07 '17 at 22:28
  • see https://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef to handle that notice. – Federkun Sep 07 '17 at 22:30

0 Answers0