【问题标题】:Sending onclick event to another page将 onclick 事件发送到另一个页面
【发布时间】: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:&nbsp <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


【解决方案1】:

而不是:

echo '<script type="text/javascript"> window.location="index2.php";</script>';

试着放:

header("Location: index2.php");

另外,请尝试添加以下说明:

在 PHP 代码的开头(第一个标签):

ob_start();

在 PHP 代码的末尾(Last Tag);

ob_flush();

【讨论】:

  • 在存在多个位置时强制页面重定向,并防止有时出现的警告:警告:无法修改标头信息 - 标头已发送(输出开始于 /www/file.php: ...) 在 /www/file.php 在线 ...
  • 是的,但您为什么认为 javascript 重定向有问题?
  • 有时,在服务器端的某些语句以及指令和代码位置下方,使用 Javascript 的重定向被阻止。
  • Javascript 重定向在服务器端被阻止?!这甚至是不可能的。
  • 您的代码显示正确,登录后尝试刷新您的页面(大小写正确)
【解决方案2】:

根据您的登录会话显示您想要的内容。这是一个基本示例的 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
}
?>

【讨论】:

  • @user3734349 很高兴听到这个消息!我希望你也理解它是如何工作的。祝你好运!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-16
  • 2010-09-22
  • 1970-01-01
  • 2013-01-24
相关资源
最近更新 更多