Form Not Submitting after Jquery Validation - java

$('form#injuryReportSubmit').submit(function(){
if(($('form#injuryReportSubmit textarea.description').val().length <=0) || ($('form#injuryReportSubmit select.part').val()== "choose one...") ||($('form#injuryReportSubmit select.type').val() == "choose one...") || ($('form#injuryReportSubmit select.weather').val()=="choose one..."));
{
$('div#errorMessage').show();
return false;
}
});
The code above is used to validate a form before it submits. The problem is the form will not submit even when all the test are false. Can someone help?
the form is on a jsp and looks like
<form id ="injuryReportSubmit" method ="post" action="injuryReportingPage.html" >
<p class ="first" >Date Of Injury</p>
<p class ="second">Date Reported to Manager</p>
<input type="text" id="dateOfInjury" name="dateOfInjury">
<input type="text" id="dateReported" name ="dateReported">
<p class ="weather">Weather</p>
<p class ="injuryType">Injury Type</p>
<p class ="bodyPart">Body Part</p>
<p class ="time">Time Injury Occurred</p>
<p class ="description">Description</p>
<select class ="weather" name="weather">
<%if(InjuryReportController.getWeatherList() != null){ %>
<% for(Weather weather : InjuryReportController.getWeatherList()){%>
<option><%= weather.getWeatherCondition() %></option>
<%} }%>
<option >choose one...</option>
</select>
<select class ="type" name="injuryType">
<%if(InjuryReportController.getInjuryTypeList() != null){ %>
<% for(InjuryType injuryType : InjuryReportController.getInjuryTypeList()){%>
<option><%= injuryType.getInjuryTypeName() %></option>
<%} }%>
<option>choose one...</option>
</select>
<select class ="part" name="bodyPart">
<%if(InjuryReportController.getBodyPartList() != null){ %>
<% for(BodyPart bodyPart : InjuryReportController.getBodyPartList()){%>
<option><%= bodyPart.getBodyPartName() %></option>
<%} }%>
<option >choose one...</option>
</select>
<input type="text" id="timeP" name ="timeOfInjury" value="01:00 AM">
<textarea class ="description" rows="120" cols="670" name="description"></textarea>
<input id ="report" value="Submit Report" type ="submit">
</form>

Remove the semicolon from the end of line 5 of your code.
Currently what you've got is like this:
if (/*your conditions*/); // <- note the semicolon
{
...
return false;
}
This means that the block with the curly braces is not associated with the if statement and so will execute every time. Obviously then returning false every time cancels every submit.

Related

How can I save all the data from different textboxs in an array, using JSTL?

I want to recieve any number of textbox values, and save it to an array with JSTL if it's possible.
I generate all the texbox where numberAsked can be any number.
<c:if test="${param.buttonSend != null}">
<form action="index.jsp" method="post">
<c:forEach var = "i" begin = "1" end = "${param.numberAsked}">
<input type="text" name="textbox[]" class="form-control">
</c:forEach>
<button type="submit" name="buttonSave">Save</button>
</form>
</c:if>
Now I want to save all the textboxs in an array.
<c:if test="${param.buttonSave != null}">
<c:set var="data" value="${request.getParameterValues(textbox)}"/>
<c:forEach var = "item" items="${param.data}">
<c:out value="${item}"/>
</c:forEach>
</c:if>
But, it doesn't work, how can I save all the data from all the generated textboxs in an array?.
Here is a demonstration JSP.
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<form method="post">
<c:forEach var = "i" begin = "0" end = "5">
<input type="text" name="textbox">
</c:forEach>
<button type="submit" name="buttonSave">Save</button>
</form>
<c:set var="data" value="${paramValues.textbox}"/>
<c:forEach var = "item" items="${data}">
${item}
</c:forEach>

pass jsp parameter to another using button

so i'm trying to pass selected value from a combobox in a jsp to another jsp here is what i got myself into
<form name="ff" method="post">
<select name='mat' id='soflow'>
<% ArrayList<Matiere> listeM = MatiereListe.GetMatiere(); %>
<option></option>
<% for (Matiere d : listeM)
{ %>
<option value= <%=d.getCode_mat() %> >
<%=d.getLib_mat() %>
</option>
<% } %>
</select>
<input type=submit value=valider />
<br>
</form>
<button onclick="window.location.href='/acceuil.jsp?mat=' JSGetSelectedItemMat() " >Statistique</button>
<script>
function JSGetSelectedItemMat()
{
var e = document.getElementById("soflow");
var strSel = e.options[e.selectedIndex].value;
document.getElementById("btt").value = strSel;
}
</script>
try this code to get selected valued from select tag. As there is no need to use js, you are using post method in your html form. Also get the value in acceuil.jsp using getParameter method. that is request.getParameter("mat");
<form action="acceuil.jsp" name="ff" method="post">
<select name='mat' id='soflow'>
<% ArrayList<Matiere> listeM = MatiereListe.GetMatiere(); %>
<option></option>
<% for (Matiere d : listeM)
{ %>
<option value= <%=d.getCode_mat() %> >
<%=d.getLib_mat() %>
</option>
<% } %>
</select>
<input type=submit value=valider />
<br>
</form>

Moving to different servlet other than form action

I am having a form :
<form action="sendaddnotification" method="post">
<%String namee=rs.getString(2);%>
<input name="IndUserName" type="hidden" value="<%=namee%>"/>
User Name : <%=namee%>
<br>
First Name : <%=rs.getString(4)%>
<br>
Last Name : <%=rs.getString(5)%>
<br>
Email Id : <%=rs.getString(6)%>
<br>
Contact : <%=rs.getString(7)%>
<br>
<%
String groupidd = request.getSession().getAttribute("groupid").toString();
s=null;
rs=null;
int flag=0;
String sql="select * from TBGROUPUSERS where I_ID=? and GU_GROUPID=?";
s = con.prepareStatement(sql);
s.setString(1,idperson);
s.setString(2,groupidd);
rs=s.executeQuery();
if(rs.next())
flag=1;
request.setAttribute("flag", flag);
%>
<c:choose>
<c:when test="${requestScope.flag == 1}">
<!-- flag is 1 -->
<input type="submit" value="REQUEST SENT" disabled="disabled"></input>
<a href="CancelRequest?userid=<%=idperson%>&userrnamee=<%=namee%>" onclick="return confirm('Are you sure you want to cancel the request?');">
<input type="submit" value="CANCEL REQUEST"></input>
</a>
</c:when>
<c:otherwise>
<!-- flag isn't 1 -->
<input type="submit" value="ADD"></input>
</c:otherwise>
</c:choose>
<input type="button" value="BACK"></input>
</form>
Now ,In this part of code :
<a href="CancelRequest?userid=<%=idperson%>&userrnamee=<%=namee%>" onclick="return confirm('Are you sure you want to cancel the request?');">
<input type="submit" value="CANCEL REQUEST"></input>
</a>
I want to move to servlet CancelRequest.java with this given parameters.But as the form action goes to sendaddnotification,So this servlet never runs.
How to make it run on click of this button.Please help
Assuming your CancelRequest servlet is defined in web.xml as:
<servlet>
<servlet-name>cancelRequest</servlet-name>
<servlet-path>packageName.CancelRequest</servlet-path>
</servlet>
<servlet-mapping>
<servlet-name>cancelRequest</servlet-name>
<url-pattern>/cancelRequest</url-pattern>
</servlet-mapping>
Write javascript function as:
function cancelRequest(){
var exit = confirm('Are you sure you want to cancel the request?');
if(exit == true){
document.getElementById('sendaddnotificationId').action = 'cancelRequest';
}else{
return;
}
}
Include id for form in jsp as:
<form action="sendaddnotification" id="sendaddnotificationId" method="post">
And change anchor element as:
<a href="cancelRequest?userid=<%=idperson%>&userrnamee=<%=namee%>" onclick="cancelRequest();">
<input type="submit" value="CANCEL REQUEST"></input>
</a>

how to display value in dropdown list depending on another dropdownlist using jsp?

I'm tying to bind the value to dropdownlist i.e named as subtype which is depending on another dropdownlist i.e named as type. Please tell me how I can do that.. I have given I following code which implement for binding the dropdownlist depending on another dropdownlist.
`<table>
<tr>
<td>
<label id="type">Chosen category : </label>
</td>
<td>
<% String getType=request.getParameter("id");
out.println("<h4>"+getType+" <a href='post free ads.jsp'>Change the
category</a> </h4>");
%>
</td>
</tr>
<tr>
<td>
<label id="typeselect">Select type : </label>
</td>
<td>
<select name="type">
<option value="none">Select</option>
<%
Connection conn = NewClass.getOracleConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("Select distinct company from admin.all_stuff
where stuff_type='"+getType+"' ");
while(rs.next()){
%>
<option value="<%=rs.getString(1)%>"><%=rs.getString(1)%></option>
<%
}
%>
</select>
</td>
</tr>
<tr>
<td> <label id="subtypeselect">Select Subtype : </label></td>
<td>
<select name="subtype">
<option value="none">Select</option>
<%
try
{ %>
<% String typeselected = request.getParameter("type");
Statement stmtmt = conn.createStatement();
ResultSet rse = stmtmt.executeQuery("Select model_no from admin.all_stuff
where stuff_type='"+getType+"' and company='"+typeselected+"' ");
while(rse.next()){
%>
<option value="<%=rse.getString(1)%>"><%=rse.getString(1)%></option>
<%
}
%>
<% }
catch(Exception e)
{
out.println(e.getMessage());
}
%>
}
</select>
</td>
</tr>
</table>`
You need AJAX for this to get it work.
Your jsp is compiled into a servlet and then delivered to your client. After that no more changes are possible.
You have to handle an onChange event on the first drop down box in HTML and then post an AJAX request to your server to send back the content for the second drop down box.
Easiest way to start with AJAX is to get some library for example jQuery
$.get('ajax/test.html', function(data) {
alert(data);
});
This code allow us to download from HTML page some content from URL 'ajax/test.html'.
Second argument is callback function. Variable data has content of page 'ajax/test.html'
More: http://api.jquery.com/jQuery.get/

How can I check for gender value by NameID?

I have this code that checks duplicate name id
NameRefBean myNameBean = new NameRefBean();
myNameBean.load(NameID);
if (!myNameBean.getErrFlag()) {
errExistFlag = true;
errCode = DUPLICATE_NAME_ID;
return(false);
I have switch case where it saves data from the jsp page form
case ACTION_SAVE:
if (hdTxnType.equalsIgnoreCase("Add")) {
if (validateInputData()) {
nameBean.setNameID(tbNameID);
nameBean.setName(tbName);
nameBean.setGender(tbGender);
hdTxnType = new String("Update");
}
} else {
nameBean.setNameID(hdNameID);
nameBean.setName(tbName);
nameBean.setGender(tbGender);
}
break;
My question is how can I check for gender value when particular nameid is selected? If user changes gender value from M to F, I need to give a warning on the jsp page saying "gender already exist do you want to modify it?"
Here is my jsp page
<% if (nameBean.getErrFlag()) {%>
<CENTER><b><font color=red><%= nameBean.getErrMsg() %></b></font></CENTER>
<% } %>
<TABLE WIDTH="800" BORDER="0">
<TR>
<TD><B>Name ID: </B></TD>
<% if (nameBean.getTxnType().equalsIgnoreCase("Add"))
{ %>
<TD><INPUT TYPE=TEXT NAME="tbNameID" VALUE="<%= nameBean.getnameID()%>" ></TD>
<% } else { %>
<TD><%= nameBean.getNameID()%> </TD>
<% } %>
<TD><B> Name: </B></TD>
<TD><INPUT TYPE=TEXT NAME="tbName" VALUE="<%= nameBean.getName()%>" ></TD>
<TD><B>Gender: </B></TD>
<TD><INPUT TYPE=TEXT NAME="tbGender" VALUE="<%= nameBean.getGender()%>" ></TD>
</TR>
<BR>
<TR>
<% if (userProfileBean.hasRole("FULL") ) { %>
<TD>
<INPUT TYPE=SUBMIT NAME="action" VALUE="Save Changes">
<% if (nameBean.getTxnType().equalsIgnoreCase("Update"))
{ %>
<INPUT TYPE=SUBMIT NAME="action" VALUE="Delete">
<% } %>
<INPUT TYPE=SUBMIT NAME="action" VALUE="Cancel">
</TD>
<% } %>
You could add a piece of JavaScript that print the warning, if a user changed the gender. The only thing you have to do then, is to enable this java script if the nameid is already set (then the name already exists, and every change should print the warning.) If the nameid is null, then it is a new name and you must not enable the java script warning functionality.

Categories

Resources