基本上我有这个PHP代码,它是一个不使用MySQL但在PHP代码中使用预定值的网页的登录系统。我正在运行php 5.5.3。
我设计的网页名为access.php
。如果您正确输入预定义的用户名和密码,则会转到user.php
页面,但如果其中任何一个不正确,则会出现一个警告框:“密码或用户名不正确”
然而,我遇到的问题是,当该警报框出现时,它会用灰色填充同一页面(access.php
),警报框位于中间,丢失所有初始网页设计,并且然后当您按下“确定”键接受警告框时它会再次带您回到access.php
页面设计。我希望这个警告框出现在我已经设计的页面上,而不会丢失任何初始设计。
以下是PHP的代码:
<?php
session_start();
if (isset($_POST['username'])) {
// Set variables to represent data from database
$dbUsname = "adminDJ";
$dbPassword = "admin";
$uid = "1111";
// Set the posted data from the form into local variables
$usname = strip_tags($_POST['username']);
$paswd = strip_tags($_POST['password']);
// Check if the username and the password they entered was correct
if ($usname == $dbUsname && $paswd == $dbPassword) {
// Set session
$_SESSION['username'] = $usname;
$_SESSION['id'] = $uid;
// Now direct to users feed
header("Location: user.php");
} else {
print 'incorrect username or password.';
}
}
?>
以下是HTML标记:
<form id="form" action="access.php" method="post"enctype="multipart/formdata">
<h2>DJ Access</h2>
<div class="lineSpacer"></div>
<p>Username <input type="text" name="username" id="userBox"/></p> <br />
<p>Password <input type="password" name="password" id="passBox"/></p> <br />
<input type="submit" value="Login to DJ Access" name="Submit" id="submit"/>
<div class="lineSpacer"></div>
</form>
我有什么方法可以拥有它,以便PHP在同一页面内提醒一个方框或使用JavaScript提醒一个方框?
答案 0 :(得分:2)
如果上面的php代码在esame页面access.php上,那么而不是打印设置变量然后用来显示消息:
<?php
session_start();
$error = false;
......
} else {
$error = true;
}
然后在表格之后:
<form id="form" action="access.php" method="post"enctype="multipart/formdata">
<h2>DJ Access</h2>
<div class="lineSpacer"></div>
<p>Username <input type="text" name="username" id="userBox"/></p> <br />
<p>Password <input type="password" name="password" id="passBox"/></p> <br />
<input type="submit" value="Login to DJ Access" name="Submit" id="submit"/>
<div class="lineSpacer"></div>
</form>
<?php if($error){ ?>
<div class="error"> There was an issue with the form")</div>
<?php } ?>
或者如果您想要提醒
<?php if($error){ ?>
<script> alert ("There was an issue with the form")</script>
<?php } ?>
答案 1 :(得分:0)
刚刚删除了放入JS的php,然后打开php agsin。将它放在您希望框出现在代码
中的位置 ?><script> alert("incorrect details"); window.history.back();</script><?php
这应该有效。 :)
答案 2 :(得分:0)
您的代码似乎不应该像您描述的那样,但这是一个使用setTimeout()
的想法:
<强> access.php 强>
<?php
session_start();
if (isset($_POST['username'])) {
// Set variables to represent data from database
$dbUsname = "adminDJ";
$dbPassword = "admin";
$uid = "1111";
// Set the posted data from the form into local variables
$usname = strip_tags($_POST['username']);
$paswd = strip_tags($_POST['password']);
// Check if the username and the password they entered was correct
if ($usname == $dbUsname && $paswd == $dbPassword) {
// Set session
$_SESSION['username'] = $usname;
$_SESSION['id'] = $uid;
// Now direct to users feed
header("Location: user.php");
} else {
// use a setTimeout to display the alert after 100ms
print 'setTimeout(function(){alert(\'whatever you want\');}, 100)';
}
}
?>
<form id="form" action="access.php" method="post"enctype="multipart/formdata">
<h2>DJ Access</h2>
<div class="lineSpacer"></div>
<p>Username <input type="text" name="username" id="userBox"/></p> <br />
<p>Password <input type="password" name="password" id="passBox"/></p> <br />
<input type="submit" value="Login to DJ Access" name="Submit" id="submit"/>
<div class="lineSpacer"></div>
</form>