1
votes

I am trying to propagate the value in input type[text] whenever user is selecting one option from select box.

I have a form containing one select option and 3 input type(text) field. What i am trying to do here whenever user select one option from select box depending upon that option value some data i want to fetch from database and propagate to the same page in input type and then after clicking on submit button i want to store the data in database.

I have used ajax call for the same but the problem is that on selecting option value i am able to get the particular data but i am not able to get the same data in jsp input type, each time i need to reload the page manually.

userdetail.jsp

//imports are here

     <html>
      <head>
      </head>
      <body>

     <script src="js/jquery.js" type="text/javascript"></script>
    <script type="text/javascript">
          $(document).ready(function(){
              $(".name").change(function(){
                  var name = $(this).val();
                  if(name.length >= 1){
                      $(".status").html("<font color=gray> Checking availability...</font>");
                       $.ajax({
                          type: "POST",
                          url: "getparamfromdb.do",
                          data: "name="+name,
                          success: function(msg){

                              $(".status").ajaxComplete(function(event, request, settings){

                                  $(".status").html(msg);

                              });
                          }
                      }); 
                  }
                  else{

                      $(".status").html("<font color=red>Username should be atleast <b>5</b> character              long.</font>");
                  }

              });
          });
        </script>

        <div class="add">

     <form:form   method="post" action="addHealthParam.do" >
     <table style="margin-top:-6%; margin-left:8%;">
     Parameter Name:<br>
     <select name="name" class="name">
    <option value="select">Select</option>
     <option value="Glucose, Serum">Glucose, Serum</option>
     <option value="Uric Acid, Serum">Uric Acid, Serum</option>
     <option value="Platelets">Platelets</option>
        <option value="NRBC">NRBC</option>
      </select> 
      Parameter Current Reading:
     <input type="text" name="current_reading"/>

      Unit of Measurement:
      <select name="measurementunit" >
      <option>
      <c:forEach var="healthp" items="${paramvalue}">
               ${healthp.measurementunit}
                    </c:forEach>

      </option>
      </select>
       Reference Range(Minimum):
         <input type="text" name="minrange" value="
         <c:forEach var="healthp" items="${paramvalue}">
          ${healthp.minrange}
          </c:forEach>" >
        Reference Range(Maximum):<br>
       <input type="text" name="maxrange" 
        value="
       <c:forEach var="healthp" items="${paramvalue}">
               ${healthp.maxrange}
        </c:forEach>">


     <input type="submit" value="Submit"/>
     </body>
     </html>    

Controller class:-

 imports are here

    @Controller
    @SessionAttributes
    public class HealthParameterController {

    @Autowired
    BharatService service;
    @Autowired
    HibernateTemplate hibernateTemp;
    List<HealthParameter> healthparam=null;
    // to store the value in database.
    @RequestMapping(value="/addHealthParam.do",method=RequestMethod.POST)
    public String addHealthParam(@ModelAttribute("addhealthparam")HealthParamMVC                  addhealthparam,BindingResult result,HttpSession session){

            // code to store the value in database 

        }
    // execute through ajax call

    @RequestMapping(value="/getparamfromdb.do",method=RequestMethod.POST)
         public String gettingParam(@ModelAttribute("addhealthparam")
                 HealthParamMVC addhealthparam,BindingResult result,
                 HttpServletRequest req,HttpSession session){
        System.out.println("In gettingParam ");
        String name=addhealthparam.getName();


     List<ParameterFromDB> li=service.getParamfromDb(name);
             // further getting value from database based on name.
         Iterator it=li.iterator();
         while(it.hasNext()){
             Object ob=it.next();
            ParameterFromDB db= (ParameterFromDB) ob;
             }
           session.setAttribute("paramvalue", li);
         // i have tried it with request also
             //request.setAttribute("paramvalue", li);
             return "userdetails";  



       }
       }
1
where is your input type declared in HTML? is it current_reading? if yes then you need to set that input type. declare current_reading as ID and then set $("#current_reading").val(msg); in javascriptJignesh
@Jani yaa this is the same question i have added code hereuser3843607
@Jignesh as you can see current_reading will be enter by user only. depending upon Parameter Name(select) i want to populate 3 field value from database(minrange,maxrang and measurementunit)and then usercan submit by clicking on submit button. Even i have already getting these value(on console) as per select the option value but these value i will not able to pass in jsp page(need to be reload each time to get the data).user3843607

1 Answers

0
votes

Here is some modify code. please check and let me know it's working or not.

<html>
      <head>
      </head>
      <body>

     <script src="js/jquery.js" type="text/javascript"></script>
    <script type="text/javascript">
          $(document).ready(function(){
              $(".name").change(function(){
                  var name = $(this).val();
                  if(name.length >= 1){
                      $(".status").html("<font color=gray> Checking availability...</font>");
                       $.ajax({
                          type: "POST",
                          url: "getparamfromdb.do",
                          data: "name="+name,
                          success: function(msg){

                              $(".status").ajaxComplete(function(event, request, settings){
                                    var json = eval(msg);
                                  $(".status").html(msg);
                                  var minRange = json.MINRANGE;
                                  vat maxRange = json.MAXRANGE;
                                  $('#minrange').val(minRange);
                                  $('#maxRange').val(maxRange);

                              });
                          }
                      }); 
                  }
                  else{

                      $(".status").html("<font color=red>Username should be atleast <b>5</b> character              long.</font>");
                  }

              });
          });
        </script>

        <div class="add">

     <form:form   method="post" action="addHealthParam.do" >
     <table style="margin-top:-6%; margin-left:8%;">
     Parameter Name:<br>
     <select name="name" class="name">
    <option value="select">Select</option>
     <option value="Glucose, Serum">Glucose, Serum</option>
     <option value="Uric Acid, Serum">Uric Acid, Serum</option>
     <option value="Platelets">Platelets</option>
        <option value="NRBC">NRBC</option>
      </select> 
      Parameter Current Reading:
     <input type="text" name="current_reading"/>

      Unit of Measurement:
      <select name="measurementunit" >
      <option>
      <c:forEach var="healthp" items="${paramvalue}">
               ${healthp.measurementunit}
                    </c:forEach>

      </option>
      </select>
       Reference Range(Minimum):
         <input type="text" id="minrange" value="
         <c:forEach var="healthp" items="${paramvalue}">
          ${healthp.minrange}
          </c:forEach>" >
        Reference Range(Maximum):<br>
       <input type="text" id="maxrange" 
        value="
       <c:forEach var="healthp" items="${paramvalue}">
               ${healthp.maxrange}
        </c:forEach>">


     <input type="submit" value="Submit"/>
     </body>
     </html> 

i use response object here. you need to get that.

imports are here

    @Controller
    @SessionAttributes
    public class HealthParameterController {

    @Autowired
    BharatService service;
    @Autowired
    HibernateTemplate hibernateTemp;
    List<HealthParameter> healthparam=null;
    // to store the value in database.
    @RequestMapping(value="/addHealthParam.do",method=RequestMethod.POST)
    public String addHealthParam(@ModelAttribute("addhealthparam")HealthParamMVC                  addhealthparam,BindingResult result,HttpSession session){

            // code to store the value in database 

        }
    // execute through ajax call

    @RequestMapping(value="/getparamfromdb.do",method=RequestMethod.POST)
         public String gettingParam(@ModelAttribute("addhealthparam")
                 HealthParamMVC addhealthparam,BindingResult result,
                 HttpServletRequest req,HttpSession session){
        JSONObject json   = new JSONObject();
        JSONArray  jsonArray = new JSONArray();
        System.out.println("In gettingParam ");
        String name=addhealthparam.getName();


     List<ParameterFromDB> li=service.getParamfromDb(name);
             // further getting value from database based on name.
         Iterator it=li.iterator();
         while(it.hasNext()){
             Object ob=it.next();
            ParameterFromDB db= (ParameterFromDB) ob;
            String minRange = ""; // Create what you want to display in Minrange Text Box
            String maxRange = ""; // Create what you want to display in Maxrange Text Box
             }

             json.put("MINRANGE",minRange);
             json.put("MAXRANGE",maxRange);
             //Get a RESPONCE object here.
             PrintWriter out = response.getWriter();
             out.print(json.toString());
           session.setAttribute("paramvalue", li);
         // i have tried it with request also
             //request.setAttribute("paramvalue", li);
             return "userdetails";  



       }
       }