Kristal
Kristal

Reputation: 235

JSP and JavaScript with form

I have a form in a Registeration.jsp page:

form name="users" method="post" onSubmit="return checkCorrect()" action="Registeration.jsp">

The Script checkCorrect is written at the start of the page, returns true or false based on information submitted to the form (using getElementById if it matters) and the script definitely works (worked on html page without jsp before)

after the form I have this:

<% if(request.getMethod().equals("POST")){...}

And I need that the jsp part will work ONLY if and after the form is successfully submitted, but somehow the javascript script is not called and don't work and the form is always submitted.

What am I doing wrong?

edits:
there's no redirection in my page, the javascript check function, the form, and the JSP part that procces the form after submitting it are at the same page.
the jsp part is used to send the data from the form to a database.
the function:

function checkCorrect(){
var fullname=document.getElementById("fullname").value;
...
if (response.length==0)
{
return true;
}
else
{
alert ("These problems are found in your form:\n\n"+response);
return false;
}
}

then come the body and the form and then
the jsp part:

<%

if(request.getMethod().equals("POST")){

String fullname=request.getParameter("fullname");
and go on.. } % >

Solution:
check the JavaScript part really good people it doesn't have compiler so even a tiny problem there can screw up the entire project.

Upvotes: 1

Views: 17166

Answers (1)

Sarin Jacob Sunny
Sarin Jacob Sunny

Reputation: 2138

<% uname=""+request.getAttribute("username"); %>

This variable will get a value only when you load your page or refresh it.

I guess your page flow is as follows.

You have your first page with form, and onsubmit form will call javascript function as

<form name="users" method="post" onSubmit="returncheckCorrect()" action="Registeration.jsp">

then your javascript will check your answer like :

<script type="text/javascript">
function returncheckCorrect() {
    var x = document.getElementsByName("userName")
    if (your condition) {
        alert("Redirecting");
        return true;
    } else {
        alert("Not Redirecting");
        return false;
    }
}
// return false; // lol, see why indentation is useful ?
</script>

then (if (your condition == true)) your java script will redirect to a second page where you want to get the value in a scriptlet like

<% uname=""+request.getAttribute("username"); %>

Make sure your code is in this manner.

Following is the code which I tried as you said, its working

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript">
    function chkme() {
        var x = document.getElementById('textfield');
        if (x.value == 'sarin') {
            alert("success");
        } else {
            alert("failed");
        }
        return true;
    }
</script>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <form id="form1" name="form1" method="post" onsubmit="chkme();"
        action="">
        <input type="text" name="textfield" id="textfield" /> <input
            type="text" name="textfield2" id="textfield2" /> <input
            type="submit" name="button" id="button" value="Submit" />
    </form>
    <%
        if (request.getMethod().equals("POST")) {
            String textfield = request.getParameter("textfield");
            String textfield2 = request.getParameter("textfield2");
    %>
    <%=textfield%>
    <br />
    <%=textfield2%>
    <%
        }
    %>
</body>
</html>

Upvotes: 3

Related Questions