dman
dman

Reputation: 11074

PHP and JavaScript - "Logout" code works in Chrome, but not in Firefox

I have a logout function that woks correctly when using chrome. But with Firefox, it does not. From what I can tell in Firefox, the foo.setAttribute( "action", "../index.php" ); is not happening as it does correctly in Chrome. It boggles my mind why since I can't really debug this portion in Firebug.

<?php
require_once('/lock_down/php/db_login.php');
require_once('/lock_down/php/authenticate.php');

if (isset($_POST['action'])) {
    $action = $_POST['action'];
} else {
    $action = 'first_time';
}

switch($action) {
case 'first_time':
    include('/lock_down/login.html');
    break;
case 'login' :
    $username = $_POST['uname'];
    $password = $_POST['pword'];
    if (http_login($username, $password)) {
        include('/lock_down/file_server.html');
    } else { 
        include('/lock_down/login.html');
    }
    break;

case 'logoff' :
    $_POST = array();
    echo "logged off";
    include('/lock_down/login.html');
    break;

default: 
//  include('/lock_down/php/login.php');
    break;


}

?>

Excerpt from JavaScript logout routine from file_server.html:

        case "logoutButton":
        var form = document.createElement("form");
        var foo = document.createElement("input");
        foo.setAttribute( "method", "post");
        foo.setAttribute( "type", "hidden");
        foo.setAttribute( "action", "../index.php" );
        foo.setAttribute( "name", "action");
        foo.setAttribute( "value", "logoff");
        form.appendChild(foo);
        form.submit();
        break;

Again, all of this works in chrome. But in Firefox it does not logout and load ../index.php.

Upvotes: 0

Views: 726

Answers (1)

charlietfl
charlietfl

Reputation: 171690

You are setting form method and action on an input element, not the form element

foo.setAttribute( "method", "post");

should be

form.setAttribute( "method", "post");

EDIT: not sure if a form that isn't in the DOM will submit in all browsers, may need to append it also. You could make an ajax request with a redirect in success callback instead of creating a form

Upvotes: 3

Related Questions