同一页面中的多表单提交

通过传递参数值的不同获取不同的信息。首先设置3个显示表单数据的对象(text1,text2,text3),并且设置其初始值为字符串,其次通过request.getParameter()方法请求对应的表单参数,将参数赋给相应的对象,最后通过表单元素显示提交后相应的信息

遇到的问题:中文乱码,处理的方法是 string message=new String(text1.getBytes(“ISO-8859-1”), “utf-8”);

在这里插入图片描述
<%@ page language=“java” contentType=“text/html; charset=UTF-8”
pageEncoding=“UTF-8”%>
<%
String text1="";
String text2="";
String text3="";
String message="";

    %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form name="form1" method="post" action="?formid=1">
表单1:
	<input name="text1" type="text" class="text" value="<%=text1 %>">
	<input type="button" name="sumnit1" value="提交" onclick="Mycheck();">
</form>
<form name="form2" method="post" action="?formid=2">
表单2:
	<input name="text2" type="text" class="text" value="<%=text2 %>">
	<input type="button" name="submit2" value="提交" onclick="Mycheck1();">
</form>
<form name="form3" method="post" action="?formid=3">
表单3:
	<input name="text3" type="text" class="text" value="<%=text3 %>">
	<input type="button" name="submit3" value="提交" onclick="Mycheck2();">
</form>
<%
if (request.getParameter("text1") != null) {
	text1 = request.getParameter("text1");
	message = "提交了第1个表单,提交内容为" + new String(text1.getBytes("ISO-8859-1"), "utf-8") + "";
	out.print(message);
}
if (request.getParameter("text2") != null) {
	text2 = request.getParameter("text2");
	message = "提交了第2个表单,提交内容为" + new String(text2.getBytes("ISO-8859-1"), "utf-8") + "";
	out.print(message);
}
if (request.getParameter("text3") != null) {
	text3 = request.getParameter("text3");
	message = "提交了第3个表单,提交内容为" + new String(text3.getBytes("ISO-8859-1"), "utf-8") + "";
	out.print(message);
}


%>
<script type="text/javascript">
function Mycheck(){
	if(form1.text1.value==""){
		alert("请输入表单1的内容!!!");
		form1.text1.focus();
		return;
	}
	form1.submit();
}
</script>
<script type="text/javascript">
function Mycheck1(){
	if(form2.text2.value==""){
		alert("请输入表单2的内容!!!");
		form2.text2.focus();
		return;
	}
	form2.submit();
}
</script>
<script type="text/javascript">
function Mycheck2(){
	if(form3.text3.value==""){
		alert("请输入表单3的内容!!!");
		form3.text3.focus();
		return;
	}
	form3.submit();
}
</script>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/weixin_44234912/article/details/87892035