1

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.

6
  • 1
    As I understand, registeration.jsp and Registeration.jsp are same page? Also, why you should do onSubmit=checkCorrect to call it on submit, not on load. Commented Feb 27, 2012 at 8:34
  • maybe you could post the javascript you are using. Have you tested with firebug or other tool to see if checkCorrect is called? Commented Feb 27, 2012 at 8:36
  • @AdamJurczyk yes they are the same page, and i need to call it on submit because the function validates the info entered in form on submit of the form and if there's a problem with data entered i need it to not submit the form. Commented Feb 27, 2012 at 9:31
  • @roel i don't know how to use firebug and those tools maybe can you refer me to a good guide? :) also - i'm using eclipse on ubuntu 10.10 with apache tomcat 6 and mysql server Commented Feb 27, 2012 at 9:33
  • 1
    JSP is a HTML code generator. JavaScript is part of HTML. Rightclick page in browser and do View Source. This should enlighten the most starters. Commented Feb 27, 2012 at 13:52

1 Answer 1

3
<% 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>
Sign up to request clarification or add additional context in comments.

11 Comments

The page is too large, i can't copy it to here =\ it's pretty much like you wrote but the script is in the <head> section between <script language="text/JavaScript"></script> then this: <% if(request.getMethod().equals("POST")) { String fullname=request.getParameter("fullname"); String email=request.getParameter("email"); String username=request.getParameter("username"); String password=request.getParameter("password"); comes right after the form
also, i'm not redirecting to anywhere.. the javascript check function, the form, and the JSP part that procces the form after submitting it and send the data from the form to a mysql database, are all on the same page. any idea how to improve it will be great :)
@Kristal, While explaining your question its better to update your question and put comments about what all things you modified. then it will be helpful for others also...
I actually had a really really dumb mistake in the javascript part but now it works just fine :) admin can flag it as solved thanks guys
@kristal,post your corrected answer and mark it as accepted.. It will be helpful for others..
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.