我有一个登录 php 表单,当用户键入不正确的凭据时,它将输出“不正确的用户名或密码”,但是它位于导航上方网站的顶部。这是屏幕截图:http://imgur.com/IAsaMWH
我想将文本放在网站包装器和 div 中,以便我可以使用 css 对其进行编辑。
我尝试过使用 document.getElementById('log').innerHTML
但这仅在 div“log”位于 php 代码之上时有效,但是 php 必须保持在顶部,因为否则我会得到一个错误:
Warning: session_start(): Cannot send session cache limiter - headers already sent
这是我的 php/js
简而言之,我想做的就是当登录凭据错误时向用户显示一条消息“用户名或密码不正确”我需要使用 CSS 编辑这条消息,因为我需要将它重新定位到页。 如果有人愿意编辑我的代码并向我解释他们做了什么 - 将不胜感激。
这是我的 PHP 和 JS:
<?php
session_start();
include_once 'dbconnect.php';
if(isset($_SESSION['user'])!="")
{
header("Location: account.php");
}
if(isset($_POST['btn-login']))
{
$email = mysql_real_escape_string($_POST['email']);
$upass = mysql_real_escape_string($_POST['pass']);
$res=mysql_query("SELECT * FROM users WHERE email='$email'");
$row=mysql_fetch_array($res);
if($row['password']==md5($upass))
{
$_SESSION['user'] = $row['user_id'];
header("Location: account.php");
}
else
{
?>
<script>document.write('Incorrect Username or Password');</script>
<?php
}
}
?>
这是我的 HTML
<?php include"authentication/login.php";?>
<!DOCTYPE html>
<html>
<HEAD>
<title>Website | Loign</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<?php include "includes/page_sources.php"; ?>
</HEAD>
<body>
<?php include "includes/navigation.php"; ?>
<div id="wrapper">
<div id="login-form">
<form method="post">
<input type="text" name="email" placeholder="Your Email" required />
<input type="password" name="pass" placeholder="Your Password" required />
<button type="submit" name="btn-login">Log In</button>
<a href="register.php">Sign Up</a>
</form>
</div>
</div>
<?php include "includes/footer.php"; ?>
</body>
</html>
再次感谢您的提前帮助
最佳答案
您的示例中存在各种错误。将 PHP 代码放在哪里并不重要:如果您想将某些内容附加到某种 div - 然后延迟它直到 dom 加载。您最好使用 jquery 或具有可用“domReady”功能的东西,但您也可以不用它。除了你的 document.write 脚本,你可以说:
window.onload = function() { document.getElementById('something').innerHTML = 'some html'; };
上面的行将等待文档加载,现在即使 php 代码高于其他所有内容,它也能够找到 div。
关于javascript - 通过输出的 document.write 设置文本样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31347674/