所以我想创建一个简单的结果页面,让用户使用给定的代码下载他们的结果。
这是脚本:
<form action="" method="post" >
<input type="text" name="logincode">
<input type="submit" name="send">
</form>
<?php
$name = $_POST['logincode'];
$filename = $name.'/'.$name.'pdf';
header('Location: ./'$filename'');
?>
原理是当用户在输入字段中写入内容(例如(1234))并按回车键时,应该将他重定向到:
./1234/1234.pdf
我不知道我的代码中的错误在哪里。
最佳答案
几个问题,
- 您的标题应该位于@showdev 在评论中提到的其他内容之前。
- 文件名和扩展名之间缺少
.
- 尾随
''
的 header 中还存在语法错误 - 您应该退出重定向 header 。
您还应该随时检查变量,并检查文件是否存在,以便显示错误。
<?php
// check post request
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$errors = [];
// check logincode is set and a number
if (!isset($_POST['logincode']) || !is_numeric($_POST['logincode'])) {
$errors['logincode'] = 'Invalid login code';
} else {
$name = $_POST['logincode'];
// check file is found
if (!file_exists($name.'/'.$name.'.pdf')) {
$errors['logincode'] = 'Your results are not ready.';
}
// now check for empty errors
if (empty($errors)) {
exit(header('Location: ./'.$name.'/'.$name.'.pdf'));
}
}
}
?>
<form action="" method="post">
<?= (!empty($errors['logincode']) ? $errors['logincode'] : '') ?>
<input type="text" name="logincode">
<input type="submit" name="send">
</form>
关于javascript - 从 PHP 重定向到 HTML 输入值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49306524/