掲示板

How validate form with ajax?

thumbnail
12年前 に Akira Akira によって更新されました。

How validate form with ajax?

Junior Member 投稿: 80 参加年月日: 10/09/24 最新の投稿
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.
thumbnail
12年前 に Mani kandan によって更新されました。

RE: How validate form with ajax?

Expert 投稿: 492 参加年月日: 10/09/15 最新の投稿
Hi,
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("}");
        }
    }
...
}
thumbnail
12年前 に Akira Akira によって更新されました。

RE: How validate form with ajax?

Junior Member 投稿: 80 参加年月日: 10/09/24 最新の投稿
Thanks. I solved problem.
thumbnail
12年前 に Mani kandan によって更新されました。

RE: How validate form with ajax?

Expert 投稿: 492 参加年月日: 10/09/15 最新の投稿
Great emoticon