1

I want to edit some element in my database following this logic: I selected an item from a drop down list (autocomplete),and the other fields are filled directly from the database based on the fields already selected, so that I can edit the data.

how to use Ajax in JSP page to do this treatment?

$(document).ready(function() {
  $('#datepicker').datepicker();

  $('#field1').change(function()
          {

//code ajax wich select the row from database and return the values on the same form


      }
  );


});

JSP code :

<s:form  cssStyle="border:0;" validate="true" action="add" namespace="/" >
<sx:autocompleter id="field1"  label="field1" list="liste1" name="liste"></sx:autocompleter>
<s:textfield name="field2" label="field2" size="15" ></s:textfield>
<s:select style="height:30px; width:125px" name="field3" label="field3"  headerValue="Select" list="liste2"></s:select>
<s:submit style="height:30px; width:125px" name="Valider" value="Valider"></s:submit>
</s:form>

any help please ?

EDIT :

I want to pass a selected value from autocomplete field

$(document).ready(function() 
    {
      $('#field1').change(function()
        {

          var selectedValue = $('#field1 option:selected').val();
            if ($.trim(selectedValue).length > 0) 
             {

                $.ajax(
                {
                    type: 'POST', 
                    url: 'action/action1',
                    data: { field1 : selectedValue},
                    dataType: 'json',
                    async: false ,
                    contentType: 'application/json; charset=utf-8',
                    success: function(){window.alert(selectedValue);}

                 });

            }

        }
      );


    });

EDIT 2:

action :

public String query()
{
    if( !field1.equals("") || field1 != null)
    {
        Service = new ServiceImpl();
        v = new Vehicule();
        v= Service.getVehiculeByImmat(field1);
        map.put(field2.toString(), v.getfield2().toString());
        map.put(date.toString(), v.getdate().toString());

    }
                return "success";

}

struts.xml:

<action name="query" class="action.GestionVehicules" method="query">
          <result name="success" type="json">map</result>
    </action>

ajax :

$(document).ready(function() 
    {
      $('#field1').change(function()
        {

          var selectedValue = $('#field1').val();
            if ($.trim(selectedValue).length > 0) 
             {

                $.ajax(
                {
                    type: 'POST', 
                    url: '<s:url action="query"/>',
                    data: { field1: selectedValue},
                    dataType: 'json',
                    success: function(){alert(data);}

                 });

            }


        }
      );


    });    

is that true ?

EDIT 3 :

javascript :

$(document).ready(function() 
    {
      $('#field1').change(function()
        {


          var selectedValue = $('#field1').val();
            if ($.trim(selectedValue).length > 0) 
             {
          alert(selectedValue);
                $.ajax(
                {
                    type: 'POST', 
                    url  : "<s:url action='query'/>",
                    dataType : 'json',
                    data: { field1: selectedValue},
                    success: function(result){
                    // alert(result);
                     if (result.length > 0) {
                            //code to put hashmap values into textfield

                                //put map.get("key1") into textfield1 ....

                            });
                        }

                },

                 });

            }

        }
      );


    });

EDIT 4 :

javascript :

  $.each(result, function(key,value)
        {                               $("#"+key).val(value); // how can it works with dropdown list ??
    } );
lilyana
  • 141
  • 1
  • 4
  • 17

1 Answers1

0

To pass selected value (if it's selected) use a simple ajax request

$.ajax(
{
    type: 'POST', 
    url: '<s:url action="action1"/>',
    data: { field1 : selectedValue},
    //async: false ,
    success: function(data){
      alert(data);
    }
 });

In the action class create a field named field1 with getter and setter. Then you have an option to return a result type. If json result is used then this field will be serialized and added to the json object you get in the data of your success handler.

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • Please how can i pass data(hashmap) from action to json ? – lilyana Mar 19 '14 at 09:02
  • Return a `json` result. – Roman C Mar 19 '14 at 09:07
  • Your data is not `json` type, but javascript. – Roman C Mar 19 '14 at 09:37
  • how can i pass this selected value to action and then return some others values to jsp? – lilyana Mar 19 '14 at 10:08
  • See [How to send JSON data via POST (ajax) and receive json response from Struts 2 action](http://stackoverflow.com/a/20422808/573032) answer. – Roman C Mar 19 '14 at 10:15
  • thanks for your help, i want to know how can i put hashmap values into textfield and select, can you see the edited post please ? – lilyana Mar 19 '14 at 17:43
  • Set the `id="field2"` and use `$("#field2").val(result.field2);` the same is for `field3`. – Roman C Mar 19 '14 at 19:28
  • thanks for your attention, but field2 is not a simple String in the action, it is an element of hashmap, how can i use it into $("#field2").val(//code to make here ?) ? – lilyana Mar 20 '14 at 09:08
  • oh ya, but it works just with a simple textfield and not with dropdown list, please how can i select an element from my drop downlist from javascript code, can you see the last edited code ? – lilyana Mar 20 '14 at 10:15
  • I want to put some values from json as selected value in , if(key === 'marque')$('#marque option[value=' + value + ']').attr('selected', 'selected'); this code is not working ? – lilyana Mar 20 '14 at 10:41