如何更改标签';从另一个php页面中删除一个页面的文本?
问题是:我正在构建一个简单的php登录页面,用户应该输入用户名和密码,然后单击“登录”按钮。此按钮将输入的值提交到另一个处理数据库的php页面,并确保用户已注册。现在,如果他/她没有注册,那么页面将返回到登录页面,但在此之前,它会更改一些标签的文本,以通知用户输入的用户名或密码是错误的 我的问题是:第二个php页面无法访问第一个php页面的元素 这是我至今使用的代码! 第二个php页面:名为LoginSubmit.php:如何更改标签';从另一个php页面中删除一个页面的文本?,php,javascript,html,dom,Php,Javascript,Html,Dom,问题是:我正在构建一个简单的php登录页面,用户应该输入用户名和密码,然后单击“登录”按钮。此按钮将输入的值提交到另一个处理数据库的php页面,并确保用户已注册。现在,如果他/她没有注册,那么页面将返回到登录页面,但在此之前,它会更改一些标签的文本,以通知用户输入的用户名或密码是错误的 我的问题是:第二个php页面无法访问第一个php页面的元素 这是我至今使用的代码! 第二个php页面:名为LoginSubmit.php: if($row = mysqli_fetch_array($result
if($row = mysqli_fetch_array($result))
{
echo "<meta http-equiv='refresh' content='0;URL=../Home.php'>";
}
else
{
echo"<script type='text/javascript'>";
echo"parent.document.getElementById('FailureText').innerHTML = 'Yours user name or password are wrong!!';";
echo "</script>";
echo "<meta http-equiv='refresh' content='0;URL=../Login.php'>";
}
if($row=mysqli\u fetch\u数组($result))
{
回声“;
}
其他的
{
回声“;
echo“parent.document.getElementById('FailureText')。innerHTML='您的用户名或密码错误!!';”;
回声“;
回声“;
}
在第一个页面(称为Login.php),标签的定义形式如下:
<td align="center" class="LiteralProb" colspan="2" style="color:Red;">
<label ID="FailureText"></label>
</td>
它是空的,似乎不存在标签,但当发生登录错误时,应在其上显示给用户的消息
需要帮忙吗!:) 将该值存储为会话中的某种“快闪消息”。在
LoginSubmit.php
中使用:
// at the top of your script
session_start();
// when you know an error occurred
$_SESSION['failure_message'] = 'Yours user name or password are wrong!!';
在另一页上使用:
// at the top of your script
session_start();
// in your HTML part
<label ID="FailureText">
<?php print ( isset($_SESSION['failure_message']) ? $_SESSION['failure_message'] : '' ); ?>
</label>
// at the bottom of your script remove the value from the session again
// to avoid that it's displayed twice
unset($_SESSION['failure_message']);
//在脚本的顶部
会话_start();
//在HTML部分
//在脚本的底部,再次从会话中删除该值
//为了避免它显示两次
取消设置($_会话['failure_message']);
您的登录页面一点也不简单;-)
这个怎么样
if (!$validLogin) {
header('Location: http://www.example.com/login?err');
exit;
}
。。。以及:
<? if( isset($_GET['err']) ){ ?>
<div>Invalid username/password</div>
<? } ?>
无效的用户名/密码
这可以通过一些不同的方式完成:
1-您可以使用会话变量存储希望在php脚本之间共享的值:
session_start();
$_SESSION['val1']=$value1; //set the value
您可以这样检索它:
//receive on the other script
session_start();
$value1=$_SESSION['val1'];
2-在将用户发送到登录脚本时,可以使用GET(URL)传递变量
header("location: first_script_url?error=some_error_message");
您可以在登录脚本中这样检索它:
$err_msg=$_GET['error'];
3-您可以使用AJAX执行登录过程,因此您不必将用户从一个脚本重定向到另一个脚本,而是调用第二个脚本,并根据第二个脚本返回值告诉用户是否存在任何错误:
使用Jquery,如果我们传递用户信息,最好使用POST,也最好使用HTTPS(无论您选择何种方法,都应该这样做),或者至少对密码使用加密功能(这不是100%安全):
用户永远不会离开第一个脚本,因此javascript/Jquery中包含了所有变量。您确实想签出AJAX。或者在一个脚本中处理所有内容并重新加载整个页面。
$.post("second_url.php", {user: username, pass: password}),
function(data){
//data contains anything second_url.php echoed. So you want to echo a 1 for example if everything went ok.
if(data == 1){
//OK
}else{
//Something went wrong, show the user some error.
}
});