简体   繁体   English

验证后如何保留在Bootstrap PopUp表单上?

[英]How to stay on a Bootstrap PopUp form after validation?

I'm actually developing a Web site where you can register yourself from a form. 我实际上正在开发一个网站,您可以在其中注册表格。

I've create a code in PHP for managing the errors which could happen (wrong number, mail...) 我已经在PHP中创建了一个代码来管理可能发生的错误(错误的数字,邮件...)

Here is my problem : When I send the form, I cant manage to stay on it to overwrite the form with the errors encounters (if there are some). 这是我的问题:发送表单时,我无法设法保留它以覆盖遇到错误的表单(如果有的话)。

I'm using Bootstrap form the form and I have absolutely no clue of how to do that. 我使用的是Bootstrap表格,但我完全不知道该怎么做。

This is the my HTML 这是我的HTML

        <div class="modal fade" id="ins" role="dialog">
            <div class="modal-dialog">
                <div class="modal-content">
                    <form id="FormIns" action="Index.php" method="post">
                        <div class="modal-header">
                            <h4 class="text-center">Inscription</h4>
                        </div>

                        <div class="modal-body">
                          <div class="row">
                              <div class="col-md-offset-3 col-md-6">
                                  <input type="text" class="form-control" name="lastname" id="lname" placeholder="Nom" maxlength="25">
                              </div>
                          </div><br/>

                          <div class="row">
                              <div class="col-md-offset-3 col-md-6">
                                  <input type="text" class="form-control" name="firstname" id="fname" placeholder="Prénom" maxlength="25">
                              </div>
                          </div><br/>

                            <div class="row">
                                <div class="col-md-offset-3 col-md-6">
                                    <input type="text" class="form-control" name="nid" id="nidentifiant" placeholder="Identifiant" maxlength="25">
                                </div>
                            </div><br/>

                            <div class="row">
                                <div class="col-md-offset-3 col-md-6">
                                    <input type="password" class="form-control" name="npass" id="npassword" placeholder="Mot de Passe" maxlength="25">
                                </div>
                            </div><br/>

                            <div class="row">
                                <div class="col-md-offset-3 col-md-6">
                                    <input type="password" class="form-control" name="cnpass" id="cnpassword" placeholder="Confirmer Mot de Passe" maxlength="25">
                                </div>
                            </div><br/>

                            <div class="row">
                                <div class="col-md-offset-3 col-md-6">
                                    <input type="mail" class="form-control" name="mail" id="mail" placeholder="Adresse Email" maxlength="25">
                                </div>
                            </div><br/>

                            <div class="row">
                                <div class="col-md-offset-3 col-md-6">
                                    <input type="text" class="form-control" name="phone" id="phone" placeholder="Numéro de Téléphone" maxlength="25">
                                </div>
                            </div><br/>

                            <div class="modal-footer">
                                <input class="btn btn-primary" type="submit" value="Inscription" name="log">
                                <a class="btn btn-default" data-dismiss="modal">Annuler</a>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>

Firstly, you should add labels to your form fields. 首先,您应该在表单字段中添加标签。 These help with the page as well as give a good entry point for error messages. 这些帮助页面,并为错误消息提供了很好的切入点。

A simple approach would be to set the form-action to the form page, and put validation errors in an associative array with values like $errors['name'] = "Name did not match"; 一种简单的方法是将表单操作设置为表单页面,然后将验证错误放入具有$errors['name'] = "Name did not match";类的值的关联数组中$errors['name'] = "Name did not match"; and so on and so forth. 等等等等。

Then you add a <span> at the end of the label like (roughly) this: 然后,在标签的末尾添加<span> ,如下所示:

<span>
  <?php echo (isset($errors['name'])) ? $errors['name'] : "";?>
</span>
</label>

This would make PHP evaluate the errors and put them into the label so they can be easily connected to the field. 这将使PHP评估错误并将其放入标签中,以便可以轻松将其连接到字段。

Lastly, to continue, you add a redirect to the next page, if the validation is successful, so create a redirect function and send the validated data to the next page. 最后,要继续操作,如果验证成功,则将重定向添加到下一页,因此创建redirect功能并将已验证的数据发送到下一页。

€: for good effects, you can use php to append the alert or warning class to the corresponding form-control groups or input s. €:为了获得良好的效果,您可以使用php将alertwarning类附加到相应的form-control组或input s上。

If you are using a script to check that the values are right, the first thing you have to do is stop the form action. 如果使用脚本检查值是否正确,则要做的第一件事就是停止表单操作。

<script type="text/javascript">
$("#FormIns").submit(function(e){
    e.preventDefault();
// your code to check the form goes here

//once you finished and everything is right then:
$( "#FormIns" ).submit();
  });

</script>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM