Phillipa
Phillipa

Reputation: 55

send multiple text box names to one jsp page by jquery

I have two text boxes and i want to send these two text box names to one jsp page. By getting these two names in check.jsp i will do some calculation and will return the result by json to third text box, but i think i am doing wrong somewhere. Can anybody give me an idea?

<script type="text/javascript">
      $(document).ready(function() {
       $("#textbox").keyup(function () {// do i need to write another onkey up for another text box?
$.getJSON('check.jsp', {
    textboxname: this.value// here how can i send another text box name to check.jsp?
},function(data){
  // get data
 });
});
});
</script>

html

<input type="text" id="textbox" name="textboxname"/>// name goes to check.jsp
<input type="text" id="textbox1" name="textboxname1"/>// how can i send this text box's
 name to that same check.jsp
<input type="text" id="textbox2" name="textboxname2"/>// here i want to display the 
result received from check.jsp 

server side(check.jsp)

String firsttextboxname=request.getParameter("firsttextboxname");
String firsttextboxname1=request.getParameter("firsttextboxname1");
JSONObject jsonObj= new JSONObject(); 
jsonObj.put("isTrue","true");
response.setContentType("application/json");
response.getWriter().write(jsonObj.toString());

Upvotes: 0

Views: 1603

Answers (1)

j_mcnally
j_mcnally

Reputation: 6968

<input type="text" id="textbox" class="submitme" name="textboxname"/>// name goes to check.jsp
<input type="text" id="textbox1" class="submitme" name="textboxname1"/>// how can i send this text box's  

<script type="text/javascript">
     $(document).ready(function() {
       $(".submitme").keyup(function () {
         $.getJSON('check.jsp', {
           textboxname: jQuery("#textbox").val(), textboxname2: jQuery("#textbox1").val()
           },function(data){
                if (data.textboxname != "" && data.textboxname2 != "") {
                  jQuery("#textbox2").val(JSON.stringify(data));
                }
                else {
                  jQuery("#textbox2").val("");
                }
             }
         });
       });
     });
</script>

------OR AS A POST-----------

$.ajax({
  type: 'POST',
  url: 'check.jsp',
  data: { textboxname: jQuery("#textbox").val(), textboxname2: jQuery("#textbox1").val() },
  success: function(data) {
     jQuery("#textbox2").text(JSON.stringify(data));
  },
  dataType: 'JSON'
});

I'll comment to say your doing a couple things I wouldn't.

1) Why not send this data via a POST

2) Why send data everytime a key is pressed, why not bind this to a submit button?

Upvotes: 1

Related Questions