简体   繁体   English

使用 php 进行引导 5.0 验证,其中用户已存在于数据库中

[英]Bootstrap 5.0 validation with php where the user already exists in the database

my registration form has basic validation for html attributes, eg type = 'email' etc. They display nicely styled by the new feature in Bootstrap Validation.我的注册表单对 html 属性进行了基本验证,例如 type = 'email' 等。它们通过 Bootstrap 验证中的新功能很好地显示。

The problem occurs when I send the form to PHP.当我将表单发送到 PHP 时,就会出现问题。 First, the data goes through AJAX to PHP to display errors without refreshing the page, but when checking eg if the entered email is already registered with a negative result, my input is still marked as: valid (green highlight).首先,数据通过 AJAX 到 PHP 显示错误而不刷新页面,但是当检查例如输入的 email 是否已经注册时,我的输入仍然标记为有效:否定结果。 What should I do我应该怎么办

register.php寄存器.php

$success = false;
$error = false;
$mssg = "Error";

if(!isset($_POST['email']) && !isset($_POST['pass'])){
    $mssg = "Inputs are empty";
}else{

    //example result for database
    $used_email = "foo@email.com";

    // example validation
    if($_POST['email'] == $used_email){
        $error = 'email';
        $mssg = "Email is already taken!";
    }else $success=true;
    
        
}

header('Content-Type: application/json');
echo json_encode(array('success' => $success, 'error'=> $error, 'mssg' => $mssg));
exit;

 (function (){ $("#formregister").on('submit', function(ev){ const form = $(this); ev.preventDefault(); ev.stopPropagation(); var obj= new Object; Object.keys(form[0].getElementsByTagName('input')).filter(function(key) { obj[form[0][key].id] = form[0][key].value; }); if (this.checkValidity()) { $.ajax({ type: "POST", url: "register.php", data: obj, cache: false, success: function(data) { if(.data.success) { if(data.error){ $("#"+data.error).addClass("is-invalid");focus(). $("#"+data.error).next().html(data;mssg). }else $("#result"),attr("class". "alert alert-danger").html(data;mssg). }else{ $("#result");html("Success"). $("#result"),attr("class"; "alert alert-success"); } } }). }else form:find(".invalid").first();focus(). form;addClass("was-validated"); }); })();
 <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet"/> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="container"> <form class="needs-validation" id="formregister" novalidate> <div id="result"></div> <div class="mt-2"> <label for="email" class="form-label">E-mail</label> <input type="email" class="form-control" id="email" required> <div class="invalid-feedback">123</div> </div> <div class="mt-2"> <label for="pass" class="form-label">Password</label> <input type="password" class="form-control" id="pass" required> <div class="invalid-feedback"></div> </div> <div class="col-12 justify-content-end d-flex mt-4 mb-2"> <button type="submit" class="btn btn-primary">Register</button> </div> </form> </div>

If I add .is-invalid class during validation, input doesn't change its style to red.如果我在验证期间添加 .is -invalid class ,则输入不会将其样式更改为红色。

Issue img问题 img

ON the STAYS GREEN issue:关于保持绿色问题:

After the server validation you have to change the class of the input with AJAX or PHP to .is-invalid to get the WRONG red style - or .is-valid for the correct value.服务器验证后,您必须使用 AJAX 或 PHP 将输入的 class 更改为 .is -invalid以获得正确的红色样式 - 或.is-valid

You can give the user additional hints with the .invalid-feedback is also supported with these classes.您可以使用.invalid-feedback为用户提供额外的提示,这些类也支持。

Exampled can be found here:https://getbootstrap.com/docs/5.0/forms/validation/#server-side示例可以在这里找到:https://getbootstrap.com/docs/5.0/forms/validation/#server-side

Formore help we need your PHP code如需更多帮助,我们需要您的 PHP 代码

Update: you are using BS5 BETA.更新:您正在使用 BS5 BETA。

THE V5.0 is now released. V5.0 现已发布。

Get it now and hopefully they fixed the bug.现在就得到它,希望他们修复了这个错误。 https://getbootstrap.com/docs/5.0/getting-started/download/ https://getbootstrap.com/docs/5.0/getting-started/download/

I found the solution!我找到了解决方案! The :invalid and :valid attributes have style advantages over classes, so the .is-invalid class must have border attributes with !imporant at the end. :invalid:valid属性相对于类具有样式优势,因此 .is -invalid class 必须具有以! important 结尾的边框属性。

For people who will have a similar problem in the future.对于将来会遇到类似问题的人。

Need to add to boostrap.css .is-invalid and.is-valid -> !important需要添加到 boostrap.css .is -invalid and.is-valid -> !important

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

相关问题 如果用户已存在于数据库中,我想通过 php get 方法发送错误消息,然后在没有 GET 变量的情况下重置 url - i want to send error message through php get method if user already exists in database but then reset the url without GET variable Nodejs:注册用户时验证数据库中是否已经存在email - Nodejs: validate if email already exists in the database when registering user 使用PHP CodeIgniter,JQuery,AJAX在数据库中存在用户名验证 - Username verification if already exists in the database using PHP CodeIgniter, JQuery, AJAX 将PhP函数与JQuery集成-检查数据库中是否已存在用户 - Integrate a PhP function with JQuery - Check if User already exists in DB 一旦它存在,就更改Bootstrap模态选项 - Change Bootstrap modal option once it already exists Bootstrap Jquery验证+ PHP - Bootstrap Jquery validation + PHP 如果用户已存在于数据库中,如何使用 Math.random() 将其排除在随机化之外 - How to exclude a user from being randomized with Math.random() if it already exists in database PHP验证以检查记录是否存在 - PHP validation to check if record exists 如何使用PHP CodeIgniter,JQuery,AJAX检查ID验证是否已存在于数据库中 - how to Check ID verification if already exists in the database using PHP CodeIgniter, JQuery, AJAX 使用 Ajax 查看电子邮件是否已存在于数据库中 - Using Ajax to see if email already exists in database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM