显示从 php 数组到模态的注册错误



我正在尝试使用引导程序和PHP制作注册表,并将错误发布在模态中。(密码错误/电子邮件地址无效等)所有错误都放入一个数组中。我的问题是我不知道如何获取数组并在 HTML 中使用它,并从那里调用模态。另外,我不希望在单击"注册按钮"后刷新页面。

我已经从下面的链接尝试过,但没有成功。 按钮单击未在模式窗口中触发(引导) 如果出现验证错误,如何打开模态?

这是文件中的表单Register_login.php

Register_login.php

<div class="container form_container_register w-100 mt-1">
<form method="post" action="Register_login_route.php" data-toggle="modal">
<div class="form-group">
<label for="inputEmailRegister">Email adress</label>
<input type="email" class="form-control" id="inputEmailRegister" name="inputEmailRegister" aria-descridedby="emailHelp" placeholder="Enter email">
</div>
<div class="form-group">
<label for="inputPasswordRegister">Password</label>
<input type="password" class="form-control" id="inputPasswordRegister" name="inputPasswordRegister" placeholder="Enter password">
</div>
<div class="form-group">
<label for="rePasswordRegister">Retype password</label>
<input type="password" class="form-control" id="rePasswordRegister" name="rePasswordRegister" placeholder="Retype password">
</div>
<div class="container register_buttons_container w-100 justify-content-center align-items-center d-flex">
<button type="submit" name="reg_button" id="reg_button_id" value="Register" data-toggle="modal" data-target="#exampleModalCenter" class="register_btn btn w-50">Register</button>  
</div> 
</form>
</div>

以及来自 Boostrap 页面的模态:(这是放置它的正确位置吗)?

<div class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLongTitle">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>

Register_login_route.php$errors是存储错误消息的数组。我只发布了我对该数组所做的工作。如何在第一个 PHP 文件中使用会话变量?PHP 代码没有问题。$session_start() 都放在两个文件中,问题在于显示模态

if (count($errors) == 0) {
$insertPass = md5($password);
$insertQuery = "INSERT INTO physical_users (email, password) VALUES (:email,:password)";
$stmt = $conn->prepare($insertQuery);
$stmt->bindParam(":email", $email);
$stmt->bindParam(":password", $insertPass);
$result = $stmt->execute();
} else{
$_SESSION['errors'] = $errors;
header("location: Register_login.php");
}

我用js尝试了上面链接中的解决方案,但没有成功

您可以使用 jQuery AJAX 将包含表单数据的请求发送到后端函数,并从那里进行验证和返回错误。然后,您可以在成功函数中打印出来。在这种情况下,您不需要表单标记,因为它会自动刷新页面。只需获取输入的值,并在data字段中的 AJAX 调用中发送它们。 AJAX 调用示例:

$.ajax({
url: "your_php_file_name.php",
type: "POST",
dataType: "json",
data: {action:"myAction",email:someEmail,password:somePassword},
success: function(res){
console.log(res);
}
})

你的PHP代码应该看起来像这样:

if(isset($_POST["action"])){
if($_POST["action"] == "myAction"){
$email = $_POST["email"];
$password = $_POST["password"];
// Example of validation
if(strlen($password) < 6){
print "Password too short";
// Printing anything will return the printed value to the success function of your AJAX call and you can work with it. If you want to return an array instead of a string you can do `print(json_encode($array))`
}
}
}

要调用你的模态,你只需要在你的成功函数中写$('#myModal').modal()并在其中显示你的错误。希望这是有帮助的。如果您有任何问题,请发表评论。

1.) 在页面上获取错误。您可以通过在页面上将其打印为 JavaScript 变量来执行此操作。类似">

<script>var errorList = <?php echo json_encode($errors);>?; </script>`

2.) 在页面上,使用 JavaScript,检查 errorList 变量。如果里面有东西,那么调用引导模式并显示它们。类似的东西

<script>
$(document).ready(function(){
if(errorList.length > 0){
//Put the errors in the .modal-content element
$("#exampleModalCenter .modal-content").text(errorList.join(","));
//Show the modal
$("#exampleModalCenter").modal("show");
}
})
</script>

这是一个粗略的草稿——在我写这篇文章的时候吐了出来——但它应该让你走上正确的道路。

最新更新