我正在提交表单,该表单包含以下文本字段
当用户输入通用密码“ DEFAULTDOC”时,它将自动下载文件。但是,如果没有,它将显示一个警告框,提示“对不起密码错误”。
我的代码工作正常。但是
中的警报框“您现在已确认文件将自动下载。”
没有显示,但是文件下载也正在工作,数据插入也正在工作。除了显示警告框。警报框未显示的原因可能是什么?
这是我的代码:(dlform.php)
<?php
$database = 'db';
$host = 'localhost';
$username = 'username';
$password = 'password';
$e = $_POST['e'];
$p = $_POST['p'];
$f = $_POST['f'];
$l = $_POST['l'];
if($_POST['p'] == "DEFAULTDOC"){
try
{
$connect = new PDO("mysql:host=$host;dbname=$database", $username, $password);
$connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$query = "INSERT INTO dlform VALUES('','$p','$f','$l','$e')";
$data = $connect->query($query);
header('Content-type: application/vnd.ms-word');
header('Content-disposition: attachment; filename=InformationSheet.docm');
?>
<script>
alert('You are now verified the file will automatically download.');
window.location.href='downloadform.php';
</script>
<?php
}
catch(PDOException $error)
{
$error->getMessage();
}
}else{
?>
<script>
alert('Sorry your password is incorrect.');
window.location.href='downloadform.php';
</script>
<?php
}
?>
这是我的html代码:(downloadform.php)
<div class="container" style="margin: 0 auto; width: 600px;margin-top: 10%;">
<center>
<img src="images/menu_bar_logo.png" alt ="" style="width: 300px;"/></center>
<br>
<div class="row">
<b>Note:</b>Fill up the following fields to get your form<br>
<form method="post" action="dlform.php">
<br>
<div class="input-group">
<span class="input-group-addon">First name</span>
<input id="msg" type="text" class="form-control" name="f" placeholder="First name">
</div>
<div class="input-group">
<span class="input-group-addon">Last name</span>
<input id="msg" type="text" class="form-control" name="l" placeholder="Last name">
</div>
<Br>
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
<input id="email" type="email" class="form-control" name="e" placeholder="Email">
</div>
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-lock"></i></span>
<input id="password" type="password" class="form-control" name="p" placeholder="Password">
</div>
<br>
<input type="submit" name="submit" class="btn btn-primary" value="Submit">
</form>
</div>
答案 0 :(得分:2)
您不能将基于标题的重定向与页面内容混合在一起。标头告诉浏览器跳过显示页面,直接进入另一页面。
由于您使用的是Javascript显示警报,所以为什么不也使用它进行重定向:
$('input[name=textInput]').attr("oninvalid", "please, only use letters");
或者尝试这个
<script type="text/javascript">
alert("Wrong Username or Password");
location="page1.php";
</script>
称呼为
function alert($message){
echo"<script>alert($message);</script>";
}
这将起作用
,也许还可以复制此link,您也可以获取有关此codeproject
的帮助答案 1 :(得分:1)
您尝试使用html标记,而dlform.php
仅具有php代码。它不会工作。尝试将消息设置为SESSION。
您的代码
<script>
alert('You are now verified the file will automatically download.');
window.location.href='downloadform.php';
</script>
将您的代码更改为php
$_SESSION['message'] = 'You are now verified the file will automatically download.';
header('Location: downloadform.php');
不正确的密码也是如此。
然后在您的downloadform.php
中添加此代码。
<?php if (isset($_SESSION['message'])): ?>
<div class="modal" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Message</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p><?php echo $_SESSION['message']; ?></p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary">Save changes</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<?php unset($_SESSION['message']); ?>
<?php endif; ?>
此代码为引导程序样式。此代码不是警报,而是类似于警报的模式。如果有来自dlform.php
的消息,它将显示。显示消息后,请销毁消息会话,以免每次重新加载downloadform.php
时都不会出现该消息会话,除非您提交表单。