Forums de discussion
How validate form with ajax?
Akira Akira, modifié il y a 12 années.
How validate form with ajax?
Junior Member Publications: 80 Date d'inscription: 24/09/10 Publications récentes
How pass data to class Java?
<script type="text/javascript">
$(document).ready(function() {
$("#txtAccount").blur(function() {
$.ajax({
type: "POST",
url: "<%= ????? %>",
data: "{'userName':'" + $("#txtAccount").val() + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(message) {
//Set the spanChecking text letting user know if the uname is available
if(message.d == "Available"){
$("#spanChecking").css({ "color": "green", "font-weight": "bold", "font-size": "small",
"padding-left": "2px" });
$('input[type="button"]').removeAttr('disabled');
$("#spanChecking").text("");
}
else if (message.d == "Unavailable") {
if($("#hidaction").val()!="update"){
$("#spanChecking").css({ "color": "red", "font-weight": "bold", "font-size": "small",
"padding-left": "2px" });
$("#spanChecking").text("Tài kho?n dã t?n t?i");
$('input[type="button"]').attr('disabled','disabled');
}
}
},
error: function(errormessage) {
//you would not show the real error to the user - this is just to see if everything is working
$("#spanChecking").text(errormessage.responseText);
}
});
});
});
</script>
Thank in advance.
<script type="text/javascript">
$(document).ready(function() {
$("#txtAccount").blur(function() {
$.ajax({
type: "POST",
url: "<%= ????? %>",
data: "{'userName':'" + $("#txtAccount").val() + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(message) {
//Set the spanChecking text letting user know if the uname is available
if(message.d == "Available"){
$("#spanChecking").css({ "color": "green", "font-weight": "bold", "font-size": "small",
"padding-left": "2px" });
$('input[type="button"]').removeAttr('disabled');
$("#spanChecking").text("");
}
else if (message.d == "Unavailable") {
if($("#hidaction").val()!="update"){
$("#spanChecking").css({ "color": "red", "font-weight": "bold", "font-size": "small",
"padding-left": "2px" });
$("#spanChecking").text("Tài kho?n dã t?n t?i");
$('input[type="button"]').attr('disabled','disabled');
}
}
},
error: function(errormessage) {
//you would not show the real error to the user - this is just to see if everything is working
$("#spanChecking").text(errormessage.responseText);
}
});
});
});
</script>
Thank in advance.
Mani kandan, modifié il y a 12 années.
RE: How validate form with ajax?
Expert Publications: 492 Date d'inscription: 15/09/10 Publications récentes
Hi,
In the jsp file,
and in the Portlet Example:
In the jsp file,
<script type="text/javascript">
function <portlet:namespace />refreshCarMakes(){
// url to the resource myCars
var carsUrl = '<portlet:resourceURL id="myCars" />';
// send AJAX call using POST
jQuery.post(carsUrl, function(data) {
// create new content from the data
var buffer = "";
for(i = 0; i < data.cars.length; i++){
var car = data.cars[ i ];
buffer += "<option value="+car.id+">";
buffer += car.name + " wheels: " + car.wheels;
buffer += "</option>";
}
// replace content
jQuery("<portlet:namespace />selectedCarOption").html(buffer);
}, "json");
}
</script>
<input type="button" value="Load cars" onclick="<portlet:namespace />refreshCarMakes()">
<select name="selectedCar" id="<portlet:namespace />selectedCarOption"></select>
and in the Portlet Example:
public class MyPortlet extends GenericPortlet {
...
@Override
public void serveResource(ResourceRequest request, ResourceResponse response) throws PortletException, IOException {
response.setContentType("application/json");
String resourceId = request.getResourceID();
if (resourceId.equals("myCars")) {
PrintWriter writer = response.getWriter();
writer.println("{");
writer.println(" 'cars': [");
writer.println(" {'id':1,'name':'myCar','wheels'=4},");
writer.println(" {'id':2,'name':'myCar','wheels'=3},");
writer.println(" {'id':3,'name':'myCar','wheels'=2},");
writer.println(" {'id':4,'name':'myCar','wheels'=1}");
writer.println(" ]");
writer.println("}");
}
}
...
}
Akira Akira, modifié il y a 12 années.
RE: How validate form with ajax?
Junior Member Publications: 80 Date d'inscription: 24/09/10 Publications récentes
Thanks. I solved problem.
Mani kandan, modifié il y a 12 années.
RE: How validate form with ajax?
Expert Publications: 492 Date d'inscription: 15/09/10 Publications récentes
Great