jqueryajax

Why clicking on submit button is nothing happens in JSP?


I have written a small code which inclued 2 JSP pages and

  1. test2.jsp --> I have made a dropdown in this page

  2. test3.jsp --> displays a welcome message when the person selects an option from the dropdown, then will migrate to this page without refreshing test2.jsp

For this I have used Ajax jQuery.

When I click on the submit button, nothing happens.

Please, help me with this.

I have also put the org.json.jar file in the libraries.

test2.jsp code:

<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="//malsup.github.com/jquery.form.js"></script>
    <script>
       function check()
       {     
$.ajax({
 url: "test3.jsp",
 type: "GET",

success: function() {
    alert("succes");
},

error: function() {
    alert( "Sorry, there was a problem!" );
}
});           
       }           
    </script>
    
</head>
<body>
    <h1>Hello World!</h1>
    <br>
    <form method="post" action="test2.jsp">
        <select name="city">
            <option value="dropdown">select city</option>
               <option value="jal">Jalandhar</option>
               <option value="ggn">Gurgaon</option>
               <option value="noida">Noida</option>
               <option value="amrtsr">Amritsar</option>
               <option value="bombay">Bombay</option>
        </select>
        <input type="submit" value="Submit" onclick="check()">
    </form>

test3.jsp code

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
</head>
<body>
    <h1>welcome</h1>
</body>
</html>

Solution

  • It should be

    <input type="button" value="Submit" onclick="check()">
    

    instead of

    <input type="submit" value="Submit" onclick="check()">
    

    because submit input type call form's action that is test2.jsp.