用javascript显示php结果的弹出窗口 [英] Popup window to display result from php with javascript
问题描述
我正在尝试实现javascript来显示来自php的结果。
I'm trying to implement javascript to display result from php.
基本上,我有一个登录页面。对于失败登录,我不希望用php回显它们,而是希望结果显示在弹出窗口中。
Basically, I have a login page. For fail login, instead of just echoing them with php, I want the result to be display in a popup.
我尝试实现警告框,但看起来我错过了什么。
I try to implement the alert box but looks like I miss something.
成功登录将重定向到 logged.php
并显示用户名和 LoginStatus 的详细信息。
Successful login will be redirect to logged.php
and will display the details of UserName and LoginStatus.
如果登录失败,由于我正在使用存储过程, LoginStatus
将自动显示失败操作的错误消息。
If the login failed, since I'm using stored procedure the LoginStatus
will automatically displayed the error message of the fail operation.
这是我的完整代码小提琴
这是我的登录页面。
login.php
login.php
$stmt=odbc_exec($conn,"CALL UserLogin (".$_POST['UserId'].",'".$_POST['UserPwd']."','".$_POST['ModuleCd']."','".$_POST['SubModuleCd']."')");
if (!$stmt)
{
"Error : " . odbc_errormsg();
}
if (odbc_fetch_row($stmt))
{
$Username=odbc_result($stmt,"Username");
$LoginStatus=odbc_result($stmt,"LoginStatus");
}
/*Succesful Login*/
if ($LoginStatus==1)
{
$_SESSION["Username"]=$Username;
$_SESSION["LoginStatus"]=$LoginStatus;
header("Location: logged.php");
}
else
/*Fail Login*/
echo $Username=odbc_result($stmt,"Username");
echo $LoginStatus=odbc_result($stmt,"LoginStatus");
html:
<form method="post" name="login">
<input type="text" name="UserId" value="">
<input type="password" name="UserPwd" value="">
<input type="submit" name="login" value="Login">
</form>
javascript:
javascript :
<script type="text/javascript">
/*message of fail login*/
function show_alert() {
if (LoginStatus != 1) {
alert(LoginStatus);
}
</script>
有任何建议吗?
推荐答案
您应该从php返回$ LoginStatus的值,而不是使用重定向。
此外,选择
不需要像这样的值属性< select value =>
是不正确的。
现在检查这个答案
You should return the value of $LoginStatus from php instead of using redirection.
Also, select
didn't need a value attribute like <select value="">
is incorrect.
Now check this answer
function xhr(){
var un=document.getElementById('UserId').value;
var pw=document.getElementById('UserPwd').value;
var md=document.getElementById('ModuleCd').value;
var smd=document.getElementById('SubModuleCd').value;
//assume that x is the XHR object
x = new XMLHttpRequest();
x.open("POST","login.php",true);
x.setRequestHeader("Content-type","application/x-www-form-urlencoded");
var params="UserId="+un+"&UserPwd="+pw+"&ModuleCd="+md+"&SubModuleCd="+smd;
x.send(params);
x.onreadystatechange=function(){
if(x.readyState==4 && x.status==200){
console.log(x.responseText);
checkLogin(x.responseText);
}
}
}
function checkLogin(x){
if(x != 1)
alert("Login failed");
else
window.location.href="logged.php";
}
<?php
session_start();
$conn=odbc_connect("DSN", " ", " ");
if (!$conn)
{
echo "Connection Failed : " . $conn;
exit(0);
}
$stmt=odbc_exec($conn,"CALL UserLogin (".$_POST['UserId'].",'".$_POST['UserPwd']."','".$_POST['ModuleCd']."','".$_POST['SubModuleCd']."')");
if (!$stmt)
{
echo "Error : " . odbc_errormsg();
}
if (odbc_fetch_row($stmt))
{
$Username=odbc_result($stmt,"Username");
$LoginStatus=odbc_result($stmt,"LoginStatus");
}
if($LoginStatus==1)
{
$_SESSION["Username"]=$Username;
$_SESSION["LoginStatus"]=$LoginStatus;
echo $LoginStatus;
}
/*
else
echo $Username=odbc_result($stmt,"Username");
echo $LoginStatus=odbc_result($stmt,"LoginStatus");
*/
?>
<form method="post" name="login">
<input type="text" name="UserId" id="UserId" value="">
<input type="password" name="UserPwd" id="UserPwd" value="">
<select name="ModuleCd" id="ModuleCd">
<option value="">Module</option>
<option value="01">01</option>
</select>
<select name="SubModuleCd" id="SubModuleCd">
<option value="">SubModule</option>
<option value="01">01</option>
</select>
<input type="button" onclick="xhr();" name="login" value="Login">
</form>
这篇关于用javascript显示php结果的弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!