Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
取消设置错误消息在php中不起作用_Php_Mysql_Ajax_Html - Fatal编程技术网

取消设置错误消息在php中不起作用

取消设置错误消息在php中不起作用,php,mysql,ajax,html,Php,Mysql,Ajax,Html,错误消息会话的取消设置不起作用。但是,当我单击带有空字段的Login按钮时,会话工作,但如果我刷新/重新加载页面,则错误消息未定义索引:消息在…显示在此行未设置($\u会话['message']) 我研究了这些问题: &我添加了这些问题中提到的解决方案,但最终还是出现了错误。 代码怎么了 home.php <?php ob_start(); session_start(); require 'connect.php'; //successful connection to db

错误消息会话的取消设置不起作用。但是,当我单击带有空字段的
Login
按钮时,会话工作,但如果我刷新/重新加载页面,则错误消息
未定义索引:消息在…
显示在此行
未设置($\u会话['message'])
我研究了这些问题:



&我添加了这些问题中提到的解决方案,但最终还是出现了错误。 代码怎么了

home.php

<?php
ob_start();
session_start();
require 'connect.php';  //successful connection to db
?>
<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript">     
        function login(file, div){
            xhttp = new XMLHttpRequest();
            xhttp.onreadystatechange = function(){
            if(xhttp.readyState==4 && xhttp.status == 200){
                document.getElementById(div).innerHTML = xhttp.responseText;
            }
        }
        xhttp.open('GET',file,true); 
        xhttp.send();       
        }
    </script>
</head>
<body> 
        <form action="loginform.php" method="GET">
            Username:<input type="text" name="uname"></br>          
            Password:<input type="password" name="pass"></br><br>
            <input type="submit" value="Login" onclick="login('loginform.php','errormsg');"><br>

            <div id="errormsg">
                <?php
                    if($_SESSION['message']){
                        echo $_SESSION['message']; 
                        unset($_SESSION['message']);//works for blank or wrong inputs but if page is refreshed error: 'Undefined index: message in...' appears in this line.
                    }
                ?>
            </div>
        </form>
</body>
</html>
<?php
ob_start();
session_start();
require 'connect.php';

if(isset($_GET['uname']) && isset($_GET['pass'])){
    $uname = $_GET['uname'];
    $pass = $_GET['pass'];

    if(!empty($uname) && !empty($pass)){ 
        $query = "SELECT `Username` FROM `userinfo` WHERE `Username`='$uname' AND `Password`='$pass'";
        $query_run=mysqli_query($dbconnect,$query);
        $num_row = mysqli_num_rows($query_run);

        if($num_row == 1){
            header('Location:welcome.php');
        }else{
            $_SESSION['message'] = 'Wrong Username or Password missing.';
            header('Location:home.php');
        }
        }else{
            $_SESSION['message'] = 'Enter values';
            header('Location:home.php');
        }
    }
?>

函数登录(文件,div){
xhttp=newXMLHttpRequest();
xhttp.onreadystatechange=函数(){
if(xhttp.readyState==4&&xhttp.status==200){
document.getElementById(div.innerHTML=xhttp.responseText;
}
}
xhttp.open('GET',file,true);
xhttp.send();
}
用户名:
密码:


loginform.php

<?php
ob_start();
session_start();
require 'connect.php';  //successful connection to db
?>
<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript">     
        function login(file, div){
            xhttp = new XMLHttpRequest();
            xhttp.onreadystatechange = function(){
            if(xhttp.readyState==4 && xhttp.status == 200){
                document.getElementById(div).innerHTML = xhttp.responseText;
            }
        }
        xhttp.open('GET',file,true); 
        xhttp.send();       
        }
    </script>
</head>
<body> 
        <form action="loginform.php" method="GET">
            Username:<input type="text" name="uname"></br>          
            Password:<input type="password" name="pass"></br><br>
            <input type="submit" value="Login" onclick="login('loginform.php','errormsg');"><br>

            <div id="errormsg">
                <?php
                    if($_SESSION['message']){
                        echo $_SESSION['message']; 
                        unset($_SESSION['message']);//works for blank or wrong inputs but if page is refreshed error: 'Undefined index: message in...' appears in this line.
                    }
                ?>
            </div>
        </form>
</body>
</html>
<?php
ob_start();
session_start();
require 'connect.php';

if(isset($_GET['uname']) && isset($_GET['pass'])){
    $uname = $_GET['uname'];
    $pass = $_GET['pass'];

    if(!empty($uname) && !empty($pass)){ 
        $query = "SELECT `Username` FROM `userinfo` WHERE `Username`='$uname' AND `Password`='$pass'";
        $query_run=mysqli_query($dbconnect,$query);
        $num_row = mysqli_num_rows($query_run);

        if($num_row == 1){
            header('Location:welcome.php');
        }else{
            $_SESSION['message'] = 'Wrong Username or Password missing.';
            header('Location:home.php');
        }
        }else{
            $_SESSION['message'] = 'Enter values';
            header('Location:home.php');
        }
    }
?>

给出错误的行在该行上方两行

你应使用:

if(isset($_SESSION['message']))
而不是:

if($_SESSION['message'])

这将检查索引是否存在,而不是值是否不是0。

home.php中更改php代码,如下所示:

<?php
        if(isset($_SESSION['message']) && $_SESSION['message'] != ''){
            echo $_SESSION['message']; 
            unset($_SESSION['message']);//works for blank or wrong inputs but if page is refreshed error: 'Undefined index: message in...' appears in this line.
        }
?>

因为您需要通过
isset()
检查会话变量
message
是否已设置


另外,有关isset()的更多信息,请查看它

在home.php中使用以下代码

<div id="errormsg">
        <?php
        if(isset($_SESSION['message']) && !empty($_SESSION['message'])){
            echo $_SESSION['message']; 
            unset($_SESSION['message']);//works for blank or wrong inputs but if page is refreshed error: 'Undefined index: message in...' appears in this line.
        }
        ?>
        </div>


希望这段代码能帮助你

我犯了一个多么愚蠢的错误……谢谢你的努力!现在问题已经解决了。