PHP登录表单中在设置错误消息后未显示错误消息。

huangapple go评论65阅读模式
英文:

php error message in login form after error message has been set is not being shown

问题

抱歉,由于您要求不提供额外的回答或解释,我将直接为您提供代码的翻译部分:

在您的login.php文件中,似乎有一个问题。您在错误消息的分配之前检查了表单数据是否已通过POST方法提交。这可能会导致错误消息在表单呈现之前被分配,因此您看不到它。以下是您的代码的翻译:

<?php
session_start();
$error_msg = null;
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    if (empty($_SESSION['uname'])) {
        $error_msg = "错误:帐户不存在";
    }
    else if ($_SESSION['uname'] == $_POST['uname'] &&
        $_SESSION['pw'] == $_POST['pw']) {
        header("Location: index.php");
    }
    else if ($_SESSION['uname'] != $_POST['uname']) {
        $error_msg = "错误:帐户不存在";
    }
}
?>
<!DOCTYPE html>
<html lang="sv">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.3/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.5/jquery.validate.min.js"></script>
<script src="../js/form-validation.js"></script>
<head>
    <title>登录</title>
    <meta charset="utf-8">
    <link rel="stylesheet" type="text/css" href="../css/style.css">
</head>
<body>
<div class="center" id="login_win">
    <form action="login_credentials.php"
          id="login_form"
          method="post"
          name="first_form">
        <h2>登录</h2>
        <?php if(isset($error_msg)) { ?>
            <p><?php echo $error_msg ?></p>
        <?php } ?>
        <label for="input_uname" id="uname_label">用户名:</label>
        <input type="text" name="uname" id="input_uname"
               placeholder="输入用户名">
        <br>
        <label for="input_pw" id="pw_label">密码:</label>
        <input type="password" name="pw" id="input_pw"
               placeholder="输入密码">
        <br>
        <button type="submit"
                id="submit_btn">登录</button>

        <div id="not_reg">还没有注册?
            <a href="signup.php" id="not_reg">注册</a>
        </div>
    </form>
</div>
</body>

希望这有助于解决您的问题。如果您需要任何进一步的翻译,请告诉我。

英文:

I'm trying to create a simple login system, and right now I want to make it so when a user enters a username, and it doesn't match a session variable (not working with database atm), there should be an error message saying that the account doesn't exist.

I have used jQuery for client side form validation,and now I want to use ajax for executing a php script to check if the username exists (i.e., is equal to the session variable) without reloading the page.

Here is the code:

login.php

&lt;?php
session_start();
$error_msg = null;
if ($_SERVER[&#39;REQUEST_METHOD&#39;] == &#39;POST&#39;) {
if (empty($_SESSION[&#39;uname&#39;])) {
$error_msg = &quot;Error: account does not exist&quot;;
}
else if ($_SESSION[&#39;uname&#39;] == $_POST[&#39;uname&#39;] &amp;&amp;
$_SESSION[&#39;pw&#39;] == $_POST[&#39;pw&#39;]) {
header(&quot;Location: index.php&quot;);
}
else if ($_SESSION[&#39;uname&#39;] != $_POST[&#39;uname&#39;]) {
$error_msg = &quot;Error: account does not exist&quot;;
}
}
?&gt;
&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;sv&quot;&gt;
&lt;script src=&quot;https://ajax.googleapis.com/ajax/libs/jquery/3.6.3/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.5/jquery.validate.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;../js/form-validation.js&quot;&gt;&lt;/script&gt;
&lt;head&gt;
&lt;title&gt; Login &lt;/title&gt;
&lt;meta charset=&quot;utf-8&quot;&gt;
&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;../css/style.css&quot;&gt;
&lt;/head&gt;
&lt;/html&gt;
&lt;body&gt;
&lt;div class=&quot;center&quot; id=&quot;login_win&quot;&gt;
&lt;!-- htmlspecialchars converts special characters to html entities
which ensures that hackers cannot inject damaging scripts
into the $_SERVER[&quot;PHP_SELF&quot;] variable --&gt;
&lt;form action=&quot;login_credentials.php&quot;
id=&quot;login_form&quot;
method=&quot;post&quot;
name=&quot;first_form&quot;&gt;
&lt;h2&gt;Login&lt;/h2&gt;
&lt;?php if(isset($error_msg)) { ?&gt;
&lt;p&gt; &lt;?php echo $error_msg ?&gt; &lt;/p&gt;
&lt;?php } ?&gt;
&lt;label for=&quot;input_uname&quot; id=&quot;uname_label&quot;&gt;Username:&lt;/label&gt;
&lt;input type=&quot;text&quot; name=&quot;uname&quot; id=&quot;input_uname&quot;
placeholder=&quot;Enter username&quot;&gt;
&lt;br&gt;
&lt;label for=&quot;input_pw&quot; id=&quot;pw_label&quot;&gt;Password:&lt;/label&gt;
&lt;input type=&quot;password&quot; name=&quot;pw&quot; id=&quot;input_pw&quot;
placeholder=&quot;Enter password&quot;&gt;
&lt;br&gt;
&lt;button type=&quot;submit&quot;
id=&quot;submit_btn&quot;&gt; Sign in &lt;/button&gt;
&lt;div id=&quot;not_reg&quot;&gt;Not registered?
&lt;a href=&quot;signup.php&quot; id=&quot;not_reg&quot;&gt;Sign up&lt;/a&gt;
&lt;/div&gt;
&lt;/form&gt;
&lt;/div&gt;
&lt;/body&gt;

css:

body {
background-image: url(&quot;nature.jpg&quot;);
background-size: cover;
background-repeat: no-repeat;
background-position: center;
background-attachment: fixed;
width: 100%;
height: 100%;
image-rendering: crisp-edges;
}
#login_form, #signup_form {
font-family: Algerian, serif;
color: #355f5f;
font-size: large;
}
.center {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: white;
width: 400px;
height: 325px;
box-shadow: 2px 2px 5px 5px rgba(0,0,0,0.15);
border-radius: 25px;
}
#login_win, #signup_win {
opacity: 85%;
}
form { margin-left: 30px; }
p { display: table-row; }
label {
float: left;
}
input {
height: 25px;
margin-left: 30px;
}
#uname_label { margin-top: 3px; }
#input_username { margin-bottom: 20px; }
#pw_label { margin-top: 50px; }
#input_pw {
margin-top: 47px;
margin-right: 50px;
}
#input_uname-error,
#input_pw-error {
color: crimson;
font-family: Monospaced, serif;
font-size: smaller;
white-space: nowrap;
}
#input_uname-error {
margin-top: -55px;
margin-left: 130px;
}
#input_pw-error {
margin-top: -55px;
margin-left: 130px;
}
.center #login_form h2,
.center #signup_form h2 {
text-align: center;
margin-bottom: 30px;
margin-right: 20px;
}
#input_email {
margin-left: 50px;
}
#submit_btn {
margin-top: 30px;
margin-left: 165px;
font-size: 16px;
}
#not_reg {
margin-top: 30px;
text-align: center;
margin-right: 15px;
}

form-validation.js:

// Wait for the DOM to be ready
$(document).ready(function() {
let data = {};
$(&#39;#login_form&#39;).submit(function(event) {
event.preventDefault();
})
.validate({
rules: {
uname:
{ minlength: 3, required: true },
pw:
{ required: true, minlength: 5 } },
messages: {
uname: { required: &quot;Please provide a username&quot;, },
pw: { required: &quot;Please provide a password&quot;, },
},
/* submit to action page when
*  the form is valid */
submitHandler: function() {
let login_form = document.getElementById(&#39;login_form&#39;);
let form_data = new FormData(login_form);
let data_arr = [];
for (const [key, value] of form_data.entries()) {
data_arr[key] = value;
}
$.ajax({
type: &quot;POST&quot;,
url: &quot;login.php&quot;,
data: { uname : data_arr[&#39;uname&#39;], pw : data_arr[&#39;pw&#39;] }
})
.done(function(msg) {
alert(msg);
});
}
});
});

As you can see above, in the login.php file I try to first initialize the error message variable, then once the form data is sent back the error message is also assigned a string text, upon which I want the if statement in the form to return true and thus the error message should be printed in the form.
So my question is, why doesn't it print, or why doesn't the error message show up in my form? The if statement should be true at that point. What have I missed? Thank you in advance.

I have tried putting the php verification code in different files. I have verified that all the if statements are true until the one that is supposed to print the error message. I was expecting the code to execute the if statement again once the error message variable had been set.

答案1

得分: 0

Thanks to the help of David and Barmar, I got it working.
I put the php code in its own file and made sure to echo the error message.

I added a div to my HTML code: <div id="error_msg"></div> where I wanted the error message to appear. Then, in the .ajax() method, I added the following code:

success: function(data) {
$("#error_msg").html(data);
}

Which takes the data (that which has been echoed) and turns it into HTML (if I understand correctly), and then it is put inside the div, which in turn shows up in my form. PHP登录表单中在设置错误消息后未显示错误消息。

英文:

Thanks to the help of David and Barmar, I got it working.
I put the php code in its own file, and made sure to echo the error message.

I added a div to my html code: &lt;div id=&quot;error_msg&quot;&gt;&lt;/div&gt; where I wanted the error message to appear. Then, in the .ajax() method, I added the following code:

success: function(data) {
$(&quot;#error_msg&quot;).html(data);
}

Which takes the data (that which has been echoed) and turns it into html, (if I understand correctly), and then it is put inside the div, which in turn shows up in my form. PHP登录表单中在设置错误消息后未显示错误消息。

huangapple
  • 本文由 发表于 2023年3月4日 02:42:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75630769.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定