简体   繁体   English

重定向到同一HTML页面,但出现错误

[英]Redirect to same Html page with error

I am trying to redirect to the same HTML page with an error message saying all fields are required if any of the fields are left empty. 我试图重定向到同一HTML页面,并显示一条错误消息,指出如果任何字段为空,则所有字段都是必需的。 I am using Servlets, MYSQL, and HTML to build this project, I am able to store data in MYSQL but stuck at this point, Please help me. 我正在使用Servlets,MYSQL和HTML来构建此项目,我能够将数据存储在MYSQL中,但此时仍卡住,请帮助我。

You can use RequestDispatcher to dispatch your HTML resource. 您可以使用RequestDispatcher调度HTML资源。 But you need to handle displaying error messages in client side (using JS). 但是您需要处理在客户端显示错误消息(使用JS)。 But it's better to use JSP, so you can achieve it easily 但是使用JSP更好,因此您可以轻松实现它

Well, since you didn't show any code, the whole idea is: 好吧,由于您没有显示任何代码,所以整个想法是:

  1. Using client side: You can use JavaScript in the client to confirm if an HTML field is empty or not 使用客户端:您可以在客户端使用JavaScript来确认HTML字段是否为空

validate.js validate.js

var field = document.forms["form_name"]["user_input"].value;

if (field==null) {
    alert("all fields are required");
    return false;
}
  1. Use server side I'm not a servlet guy but in PHP you can do this: 使用服务器端,我不是servlet的人,但是在PHP中,您可以这样做:

validate.php validate.php

if(isset($_POST["user_input])){
   $field = $_POST["user_input"];
   if(empty($field){
      echo "error, all fields are required"
   }

Hope this helps 希望这可以帮助

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

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