我有一个注销功能,在使用 Chrome 时可以正确运行。但对于 Firefox,则不然。据我所知,在 Firefox 中, foo.setAttribute( "action", "../index.php");
并没有像在 Chrome 中那样正确地发生。这让我很困惑,因为我无法在 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;
}
?>
摘自 file_server.html
中的 JavaScript 注销例程:
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;
同样,所有这些都可以在 Chrome 中运行。但在 Firefox 中,它不会注销并加载 ../index.php
。
最佳答案
您正在输入元素而不是表单元素上设置表单方法
和操作
foo.setAttribute( "method", "post");
应该是
form.setAttribute( "method", "post");
编辑:不确定不在 DOM 中的表单是否会在所有浏览器中提交,可能还需要附加它。您可以在成功回调中发出带有重定向的 ajax 请求,而不是创建表单
关于PHP 和 JavaScript - "Logout"代码在 Chrome 中有效,但在 Firefox 中无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13215110/