![](/img/trans.png)
[英]How to link to another php page using onclick event from javascript?
[英]Sending onclick event to another page
我有一個登錄頁面,我想做的是單擊按鈕時的登錄信息,登錄信息很好,它進入了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>
代替 :
echo '<script type="text/javascript"> window.location="index2.php";</script>';
嘗試把:
header("Location: index2.php");
另外,嘗試添加以下說明:
在PHP代碼的開頭(第一個標記):
ob_start();
在PHP代碼的末尾(最后一個標記);
ob_flush();
根據您的登錄會話顯示所需的內容。 這是基本示例的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
}
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.