Link to home
Create AccountLog in
Avatar of puneet kumar
puneet kumar

asked on

ajax call is not going when no error come from validate function

Hi In below function i m calling one validate function save()
if save() execute and give all values validated then ajax call go
other wise ajax call don't go . in below code save function is working
perfectly but when it gives no validation error ajax call is not going.
please help me out to recify this.


$(document).ready(function() {
	$("#save_data").click(function(evt) {
		evt.preventDefault();
		alert("succeess");
		if (save()){
			$.ajax({
				url: "Test.jsp",
				type: "post",
				data: {
				pS:$('#pS').val(),
				psT:$('#psT').val(),
				pC:$('#pC').val(),
				mY:$('#mY').val(),
				pDate:$('#pDate').val(),
				pdDate:$('#pdDate').val(),
				bDate:$('#bDate').val()},
				success: function(responseFromServer) {
	            	$.each(responseFromServer, function(resultMessageKey,resultMessageValue) {	
	                    $('#content').html(resultMessageKey);
	                });
	                //$('#content').html("Date Confguration Saved");
	    	    	//getClearDateValues();
	    		
	            },
	            error: function() {
	                alert(" Ajax call Failed to Update Values into Database");
	            }
			});
		}
	});
});

Open in new window

Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

What does the save() function look like.
Are you sure that save() is returning TRUE - there is nothing in your code that indicates you are able to detect this.
For instance add this after line 29 - does the 'Validate Failed message appear?
else {
  alert('Validate Failed');
}

Open in new window


Have you tried defining your save() function like this for testing purpose

function save() {
  return true;
}

Open in new window

Does that work? If so we need to look at your save() code to see why it is returning false
Avatar of puneet kumar
puneet kumar

ASKER

please refer below code for function save

function save()
{

if(!ValidateJScrip())
return false;
      var pdate,pdate,bdate;
      pdate = document.getElementById("pdate").value;
      pdate = document.getElementById("pdate").value;
      bdate = document.getElementById("bdate").value;
      pdate1 = Date.parse(pdate);
      pdate1 = Date.parse(pdate);
      bdate1 = Date.parse(bdate);

      if(document.forms.form2.pGroup.value == '0')
      {
            alert("Please select PGroup Code");
            document.forms.form2.pGroup.focus();
            return;
      }
      if(document.forms.form2.pType.value == '')
      {
            alert("Please select P Type");
            document.forms.form2.pType.focus();
            return;
      }
      if(document.forms.form2.pCode.value == '')
      {
            alert("Please select P Code");
            document.forms.form2.pCode.focus();
            return;
      }
      if(document.forms.form2.mYear.value == '')
      {
            alert("Please select M Year");
            document.forms.form2.mYear.focus();
            return;
      }
      if((pdate < pdate))
      {
          alert("P Date should be greater then C Date");
          document.forms.form2.pdate.focus();
    return;
      }
      if((bdate < pdate))
      {
          alert("B Date should be greater then P Date");
          document.forms.form2.bdate.focus();
    return;
      }
      if((bdate < pdate))
      {
          alert("B Date should be greater then P Date");
          document.forms.form2.bdate.focus();
    return;
      }
      
}
Nowhere in that script do you have a
return true;

Open in new window


All your returns are plain returns with no value - you need to explicitly return TRUE when you want a true value from the function.

You should also return the default at the end of the function - currently your function terminates without doing a return.
i did't get much julian can you please send some code snippet.
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Thanx julian for above solution............
You are welcome