echo login error in different page, specific div

假装没事ソ 提交于 2020-01-06 19:15:24

问题


Delayed too long with this issue. I have a index.php including form and login.php handeling it.

How do I post the errors in a specific div in index.php? Here is the Index.php container, no php code in idnex.php yet.

    <div id="container">
    <section>
        <h1>ברוכים הבאים לאתר קופונים</h1>
        <h2>המקום בו תוכלו למצוא קופונים בסביבתכם</h2>
        <hr>
    </section>

    <section id="mainpage">
            <p class="welcome">
                אנא התחבר בכדי ליצור ולראות קופונים בקרבתך</br>
                <a href="php/register.php">הירשם לאתר</a> 
            </p>

            <form action="php/login.php" method="post" class="form">
                <p class="email">
                    <input type="text" name="email" /> :דואר אלקטרוני</br>
                </p>
                <p class="password">
                    <input type="password" name="password" /> :סיסמא</br>
                </p>
                <p class="submit">  
                    <input type="submit" value="היכנס" />  
                </p>  
            </form>
    </section>
</div>

and here is the login.php error reporting section

            //check matching input
        if($email == $dbemail && $password = $dbpassword){
            $_SESSION['email'] = $dbemail;
            include('../index.php');
            header('members.php');

        }
        else{
            include('../index.php');
            echo "Incorrect Password<style text-align:center;/>";
        }
    }

} else
    include('../index.php');
    die('<p class="error">User does not exist</p>');
} else
    include('../index.php');
    die('Please enter a Email and password');

tried this

include('../index.php');
    die('<p class="error">User does not exist</p>');

can't manage to specifically position it under the Submit button (using margin: 0 auto so left and right changes)

Thanks for any help given


回答1:


change your login.php file:

} else {
   // error happened
   $error = '<p class="error">User does not exist</p>'
   include('../index.php');
   exit;
...

and your index.php file:

<form action="php/login.php" method="post" class="form">
<?php
if(isset($error)) echo $error;
?>
<p class="email">
   <input type="text" name="email" /> :דואר אלקטרוני</br>
...



回答2:


The problem is, is that your login.php file directly echo's the error message out. A better method would be to save the message into a variable - even a session variable would suffice (as it looks like you're not using OOP, by the example code).

Try updating the error messages to not use echo, but instead maybe:

$_SESSION['error_message'] = "Incorrect Password<style text-align:center;/>";

And then, in index.php, exactly where you want it to be displayed add:

     <p class="submit">  
          <input type="submit" value="היכנס" />  
     </p>  
</form>
<?php
if (isset($_SESSION['error_message'])) {
    echo $_SESSION['error_message'];
    unset($_SESSION['error_message']); // clear the message to prevent duplicate displays
}
?>

As it looks like you want to include the index.php file when the actual error occurs, you can set a local variable right before the call to include('../index.php'); inside login.php, like this:

} else {
    $errMsg = "Incorrect Password<style text-align:center;/>";
    include('../index.php');
}

And like the above example modified index.php, you can do the same here like:

    </p>
</form>
<?php if ($errMsg) { echo $errMsg; } ?>


来源:https://stackoverflow.com/questions/12112882/echo-login-error-in-different-page-specific-div

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!