【发布时间】:2014-11-05 07:30:36
【问题描述】:
我有一个登录页面,我想做的是当点击一个按钮时,登录信息很好,它进入 index.php 并隐藏日志 div 并显示 nova div。我的代码中有一些东西,但它不起作用。 这是我的代码。
登录.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>
【问题讨论】:
-
为什么不在服务器端显示每个 div,基于
if(isset($_SESSION['username']))? -
嗯,我不知道该怎么做。你有这方面的例子吗?
-
我写了一个答案,因为这里没有足够的空间发布它。希望对您有所帮助。
标签: php html forms button onclick