简体   繁体   English

弹出窗口以使用javascript显示php的结果

[英]Popup window to display result from php with javascript

I'm trying to implement javascript to display result from php. 我正在尝试实现javascript以显示来自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. 对于失败的登录,我希望结果显示在弹出窗口中,而不仅仅是用php回显它们。

I try to implement the alert box but looks like I miss something. 我尝试实现警报框,但看起来好像错过了一些东西。

Successful login will be redirect to logged.php and will display the details of UserName and LoginStatus . 成功登录将被重定向到logged.php ,并将显示UserNameLoginStatus的详细信息。

If the login failed, since I'm using stored procedure the LoginStatus will automatically displayed the error message of the fail operation. 如果登录失败,由于我正在使用存储过程,因此LoginStatus将自动显示失败操作的错误消息。

Here's my full code fiddle 这是我的完整代码小提琴

This is my login page. 这是我的登录页面。

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 : 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> 

Any suggestion? 有什么建议吗?

 <form method="post" name="login"> <input type="text" name="UserId" id="UserId" value=""> <input type="password" name="UserPwd" id="UserPwd" value=""> <input type="button" onclick="xhr();" name="login" value="Login"> </form> 

 function xhr(){ var un=document.getElementById('UserId').value; var pw=document.getElementById('UserPwd').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; x.send(params); x.onreadystatechange=function(){ if(x.readyState==4 && x.status==200){ checkLogin(x.responseText); } } } function checkLogin(x){ if(x != 1) alert("Login failed"); else window.location.href="logged.php"; } 

The PHP server page should echo only "1" or ~1 (0 or something else). PHP服务器页面应仅回显“ 1”或〜1(0或其他)。 no other echo statement must be there in the PHP page PHP页面中必须没有其他echo语句

You have to send data using Ajax to the PHP and get the answer back and show the popup message 您必须使用Ajax将数据发送到PHP,并获得答案并显示弹出消息

HTML : HTML:

<form id="login" action="#" method="POST">

            <input type="text" name="username" value="" placeholder="Username" />

            <input type="password" name="password" value="" placeholder="Password" />

            <button id="submit" > Submit </button>

</form>

JS : (U need jQuery library) JS :(您需要jQuery库)

jQuery(document).ready(function()
{
jQuery("#login").submit(function(event)
{
    jQuery.ajax(
    {
        url: "login.php",
        type: "POST",
        dataType: "html",
        data: jQuery("#login").serialize(),

        success: function(data)
        {
            if(data == "failed")
            {
                alert("LoginStatus"); // If you want send the variable LoginStatus from PHP to JS, you have to get data back using Json !
            }
            else if(data == "success")
            {
                window.location.href = "logged.php";
            }
        }
    });
    return false;
});
});

PHP : PHP的:

Use your PHP code and check whatever condition is true or false and echo it as "failed" or "success". 使用您的PHP代码并检查是否为真或假,然后将其回显为“失败”或“成功”。

You should return the value of $LoginStatus from php instead of using redirection. 您应该从php返回$ LoginStatus的值,而不要使用重定向。 Also, select didn't need a value attribute like <select value=""> is incorrect. 另外, select不需要像<select value="">这样的值属性不正确。 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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM