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 ??
} );