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

JavaScriptjQueryAJAXGolang

Avatar of undefined
Last Comment
Julian Hansen

8/22/2022 - Mon
Julian Hansen

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
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;
      }
      
}
Julian Hansen

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.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
puneet kumar

ASKER
i did't get much julian can you please send some code snippet.
ASKER CERTIFIED SOLUTION
Julian Hansen

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
puneet kumar

ASKER
Thanx julian for above solution............
Julian Hansen

You are welcome
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.