在不刷新页面的情况下重定向到PHP和JS
问题描述:
我试图制作一个web文件服务器(一个云)。我决定在index.php
上提供基本的表单密码验证。当用户输入密码并单击该按钮时,JS将该密码发送到index.php
,并确定它是否正确。在不刷新页面的情况下重定向到PHP和JS
如果是,则index.php将用户重定向到files.php
。如果不是,则刷新页面。 我知道重定向和刷新是通过header();
完成的,但是当我使用它并在我的浏览器中观看“网络”选项卡时,它会收到files.php
,但不会加载它(保持在index.php
)。我确信原因是在一些文本之后发送了头文件(好吧,它是头文件)。
当前页面发送后,如何将用户的浏览器重定向到某个页面?我应该让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';
答
你是全光照g Ajax,而Ajax的要点是在不加载新页面的情况下发出HTTP请求。
既然你想加载一个新页面:删除JavaScript。使用普通的HTML提交按钮执行常规表单提交。
+0
谢谢!这工作。 – danshat
答
透过JS如果你想直接打开它,你可以按照如下
window.open(response.data.url, '_blank');
写当前标签
window.open(response.data.url);
您是否正在使用FORM?你能提供一些代码吗? – Ivan
我们可以看到重定向代码吗? – Deckerz
您可能需要ajax或cURL – bdalina