Question

I have a search.jsp in which I can enter a number to find data in a database table. I want to pass the found data as parameters to another .jsp but ONLY if the submit button is clicked and ONLY if data was found in the table or database. Here's my code but it doesn't work correctly.

<script>
    var validation = new Boolean();

    function valid() {
        if(validation == false)
            return false;
        else {
            return true;
        }
    }
</script>

<form action="change.jsp" onsubmit="return valid();" >
    <br> <input type="text" name="searchAlbumNo"> AlbumNo </input>
    <br> <input type="submit" value="Search" name="clicked"> <br>   
</form>

if(request.getParameter("clicked") == null ||  !(request.getParameter("clicked").equals("Search")))  {
    System.out.println(request.getParameter("clicked"));
            %< <script> validation = false </script> <%
    } else {
        //... java code
        //... and if data found
        <script> validation = true </script>
    }

Now the validation thing actually works but only once and the Java code is never accessed or executed. How can I solve this problem?

Was it helpful?

Solution

i think you need to study up and first understand what is java/jsp (running on the server) ; what is javascript (running on your browser), how they can talk (look for a plain simple AJAX sample, simple form). Your mixing up Javascript and Jsp code like the occur in the same machine-process. They do not. Jsp is in the app server and javascript on the browser

You need to submit page from the html of search.jsp back to it self('search.jsp')

If the search field is empty then show the same page again (what happens when the user asks for the page first time ).

The client validation code has to be inside the javascript - probably test if the field is empty. But you cannot rely on this - it happens on the client, can be switched off or manipulated.

On the server perform the search after testing the input/ take care of sql injection etc. Then if you need to go to another page, do a send redirect (need to make sure no other output sent to client before that - not even a new line or space)

You should really first go thru a beginner book and web articles .

OTHER TIPS

The problem with the code above that is doesn't submit the form to the server to perform validation on the server with database values. To fix this you should change the action attribute to search.jsp and let the javascript perform submit. When the same page loads again you do the database validation and if everything is ok then just forward to another page change.jsp.

} else {
  //... java code
  //... and if data found
  <jsp:forward page="change.jsp"/>
}

To perform validation on the client-side with javascript you should change the function a little.

<script>
  function valid(form) {
    if (trim(form.searchAlbumNo.value).length == 0) {
      alert('Please enter a searchAlbumNo');
      form.searchAlbumNo.focus(); 
      form.searchAlbumNo.select(); 
      return false;
    } else {
      return true;
    }
  }
</script>

this prevents submitting empty field.

<form action="search.jsp" onsubmit="return valid(this);" >
  <br> <input type="text" name="searchAlbumNo"> AlbumNo </input>
  <br> <input type="submit" value="Search" name="clicked"> <br>   
</form>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top