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

puneet kumar
puneet kumar used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
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

Author

Commented:
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;
      }
      
}
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
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.
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Author

Commented:
i did't get much julian can you please send some code snippet.
Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
In your save function you don't have a default return at the end of your function i.e. NOWHERE IS YOUR FUNCTION EVER RETURNING TRUE - so the save() call will never EVALUATE to TRUE and your AJAX will NEVER fire.

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();
    // EXPLICITLY RETURN FALSE
    return false;
  }
  if(document.forms.form2.pType.value == '') {
    alert("Please select P Type");
    document.forms.form2.pType.focus();
    // EXPLICITLY RETURN FALSE
    return false;
  }
  if(document.forms.form2.pCode.value == '') {
    alert("Please select P Code");
    document.forms.form2.pCode.focus();
    // EXPLICITLY RETURN FALSE
    return false;
  }
  if(document.forms.form2.mYear.value == '') {
    alert("Please select M Year");
    document.forms.form2.mYear.focus();
    // EXPLICITLY RETURN FALSE
    return false;
  }
  if((pdate < pdate))  {
    alert("P Date should be greater then C Date");
    document.forms.form2.pdate.focus();
    // EXPLICITLY RETURN FALSE
    return false;
  }
  if((bdate < pdate))  {
    alert("B Date should be greater then P Date");
    document.forms.form2.bdate.focus();
    // EXPLICITLY RETURN FALSE
    return false;
  }
  if((bdate < pdate))  {
    alert("B Date should be greater then P Date");
    document.forms.form2.bdate.focus();
    // EXPLICITLY RETURN FALSE
    return false;
  }
  
  // ADD DEFAULT RETURN FOR FUNCTION
  // ASSUMES IF YOU GET HERE NO ERRORS
  return true;
} 

Open in new window

Author

Commented:
Thanx julian for above solution............
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
You are welcome

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial