gpt4 book ai didi

javascript - PHP和JS中的重定向而不刷新页面

转载 作者:行者123 更新时间:2023-12-03 03:34:14 24 4
gpt4 key购买 nike

我正在尝试制作一个网络文件服务器(云)。我决定在 index.php 上放置一个基本表单密码身份验证。当用户输入密码并单击按钮时,JS 将该密码发送到 index.php 并决定它是否正确。

如果是,则index.php将用户重定向到files.php。如果没有,则刷新页面。我知道重定向和刷新是通过 header(); 完成的,但是当我使用它并观看浏览器中的“网络”选项卡时,它会收到 files.php但不加载它(保留在 index.php 上)。我确信原因是在一些文本之后发送了 header (好吧,它是 header )。

发送当前页面后如何将用户的浏览器重定向到某个页面?我应该让JS来做吗?那怎么办?这是我的index.php:

<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php
echo '
<h1>Hello World</h1>
<form>
<input name="password" type="text"></input>
<button onclick=authClick() class="test" type="button"></button>
</form>
';
print_r($_POST);
if ($pass == "123123") {
header("Location: files.php");
}
?>
<script src="scripts/main.js"></script>
</body>
</html>

这是 JS:

function authClick() {
var editBox = document.querySelector('input');
var xhr = new XMLHttpRequest();
var body = 'password='+encodeURIComponent(editBox.value);
xhr.open("POST","/index.php",true);
xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
xhr.send(body);
}

var myHeading = document.querySelector('h1');
myHeading.textContent = 'nigga';
b = document.querySelector('button');
b.setAttribute('content', 'test content');
b.setAttribute('class', 'btn');
b.innerHTML = 'submit';

最佳答案

您正在使用 Ajax,Ajax 的要点是发出 HTTP 请求而不加载新页面

由于您想要加载新页面:删除 JavaScript。使用常规 HTML 提交按钮执行常规表单提交。

关于javascript - PHP和JS中的重定向而不刷新页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45943280/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com