我有一个登录页面,我想要做的是点击一个按钮,登录信息很好,它转到index.php并隐藏日志div并显示nova div。我的代码中有一些东西,但它不起作用。 这是我的代码。
的login.php
<?php
session_start();
if(isset($_SESSION['username'])) {
echo '<script type="text/javascript"> window.location="index2.php";</script>';
}
if(isset($_POST['submit'])) {
include('connection.php');
$user = mysql_real_escape_string($_POST['username']);
$pass = md5($_POST['password']);
if($user && $pass) {
$sql="SELECT * FROM korisnici WHERE username = '" . $user . "' and password = '" . $pass . "'";
if (!$q=mysql_query($sql))
{
echo "<p>Error</p>" . mysql_query();
die();
}
if (mysql_num_rows($q)==0)
{
echo '<script type="text/javascript">alert("Incorrect username and password"); window.location="login.php";</script>';
}
else {
$_SESSION['username'] = $_POST['username'];
header('Location: index.php');
}
} else {
echo "<script language='javascript'>alert('Fill out both fields!')</script>";
}
}
?>
<form action="login.php" method="POST">
Username: <input type="text" name="username" >
Password:  <input type="password" name="password" >
<input type="submit" id="submit" value="LOGIN" name="submit" class="button" onclick="showCommentDiv()"/>
</form>
app.js
function showCommentDiv() {
document.getElementById('nova').style.display = "block";
document.getElementById('log').style.display = "none";
}
的index.php
<div id="log">
<form action="login.php">
<input type="submit" id="logdugme" value="LOGIN/REGISTRACIJA" name="logdugme" class="button"/>
</form>
</div>
<div id="nova">
<form action="#" method="POST">
<colgroup>
<col widht="25%" style="vertical-align:top;"/>
<col widht="75%" style="vertical-align:top;"/>
</colgroup>
<table>
<tr>
<td><label for="comment" class="ime" >Comment :</label></td>
<td><textarea name="comment" rows="10" cols="50" maxlength="190" class="ime"></textarea></td>
</tr>
<tr><td colspan="2"><input type="submit" class="dugme" name="submit" value="Leave comment"></td></tr>
</table>
</form>
</div>
答案 0 :(得分:0)
而不是:
echo '<script type="text/javascript"> window.location="index2.php";</script>';
尝试放:
header("Location: index2.php");
另外,请尝试添加以下说明:
在PHP代码的开头(第一个标签):
ob_start();
在PHP代码(最后标记)的末尾;
ob_flush();
答案 1 :(得分:0)
根据您的登录会话显示您想要的内容。这是您的index.php文件的基本示例:
<?php
session_start();
if (!isset($_SESSION['username']))
{
?>
<div id="log">
<form action="login.php">
<input type="submit" id="logdugme" value="LOGIN/REGISTRACIJA" name="logdugme" class="button"/>
</form>
</div>
<?php
}
else
{
?>
<div id="nova">
<form action="#" method="POST">
<colgroup>
<col widht="25%" style="vertical-align:top;"/>
<col widht="75%" style="vertical-align:top;"/>
</colgroup>
<table>
<tr>
<td><label for="comment" class="ime" >Comment :</label></td>
<td><textarea name="comment" rows="10" cols="50" maxlength="190" class="ime"></textarea></td>
</tr>
<tr><td colspan="2"><input type="submit" class="dugme" name="submit" value="Leave comment"></td></tr>
</table>
</form>
</div>
<?php
}
?>