$("#form1").submit(function(e){
           e.preventDefault();
    });

    $("#submit").click(function(e){

    dataString = $("#form1").serialize();

    var new = $("input#new").val();
    dataString = "new=" + new;

      $.ajax({
                type: "POST",
                url: "Servlet",
                data: dataString,
                dataType: "json",

                 success: function( data, textStatus, jqXHR) {
                    //our country code was correct so we have some information to display
                     if(data.success){
                         $("#ajaxResponse").html("");
                           $("#ajaxResponse").append("<b>Country Code:</b> " + data.countryInfo.code + "
");
                     }
                     //display error message
                     else {
                         $("#ajaxResponse").html("<div><b>Category Name is invalid!</b></div>");
                     }
                },

                 error: function(jqXHR, textStatus, errorThrown){
                     console.log("Something really bad happened " + textStatus);
                      $("#ajaxResponse").html(jqXHR.responseText);
                },

                //capture the request before it was sent to server
                beforeSend: function(jqXHR, settings){
                    //adding some Dummy data to the request
                    settings.data += "&dummyData=whatever";
                    //disable the button until we get the response
                    $('#submit').attr("disabled", true);
                },

                //this is called after the response or error functions are finsihed
                //so that we can take some action
                complete: function(jqXHR, textStatus){
                    //enable the button
                    $('#submit').attr("disabled", false);
                }

            });      

         });

Recommended Answers

All 6 Replies

You should really explain what you are trying to do, and what specifically isn't working. Dumping code won't get you any help.

As i can see your code you define js variable as 'var new' its not correct.
i just modify your code. i don't know that what you are trying to achive. but as i understood below code fire ajax on submit click.

<html>
<head>
  <script src="//code.jquery.com/jquery-1.11.0.min.js"></script>

</head>
<body>
  <form name="form1" id="form1" method="post">
    <input type="text" name="s" id='s' />

    <input type="submit" name="submit" id='submit' value=" submit" />
  </form>
</body>
<script type="text/javascript">
  $("#form1").submit(function(e){
   e.preventDefault();
 });
  $("#submit").click(function(e){
    dataString = $("#form1").serialize();
    var new1 = $("input#new").val();
    dataString = "new=" + new1;
    $.ajax({
      type: "POST",
      url: "http://localhost/servlet",
      data: dataString,
      dataType: "json",
      success: function( data, textStatus, jqXHR) {
                    //our country code was correct so we have some information to display
                    if(data.success){
                     $("#ajaxResponse").html("");
                     $("#ajaxResponse").append("<b>Country Code:</b> " + data.countryInfo.code + "");
                   }
                     //display error message
                     else {
                       $("#ajaxResponse").html("<div><b>Category Name is invalid!</b></div>");
                     }
                   },
                   error: function(jqXHR, textStatus, errorThrown){
                     console.log("Something really bad happened " + textStatus);
                     $("#ajaxResponse").html(jqXHR.responseText);
                   },
                //capture the request before it was sent to server
                beforeSend: function(jqXHR, settings){
                    //adding some Dummy data to the request
                    settings.data += "&dummyData=whatever";
                    //disable the button until we get the response
                    $('#submit').attr("disabled", true);
                  },
                //this is called after the response or error functions are finsihed
                //so that we can take some action
                complete: function(jqXHR, textStatus){
                    //enable the button
                    $('#submit').attr("disabled", false);
                  }
                });      
});
</script>
</html>

Another viable way to do this would be to run the ajax on the submit method of the form, instead of the click event of the submit button. This way the use of 'Enter' to submit the form would also be captured.

I am trying to retrieve my values from database to the jsp page

By using checkbox to show a dropdownlist

On the click event of the checkbox, check whether it's 'checked' using $('#checkbox').is(':checked').

If you get a true, use $.load('/ajax/ddlist.aspx?rnd='+Math.random());

Be sure to use the random addition at the end (or use $.ajax with cache:false) else you may get a cached result instead of the latest updated dropdown list.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.