-1

in cf9 i have a page where i want to check if field value exist in db (via ajax). If it doesn't exist, i want to stop processing (return false). Everything works fine, except i don't know how to pass back to the main function the result of the ajax call please help

<cfajaximport tags="cfmessagebox, cfwindow, cfajaxproxy">
<cfajaxproxy cfc="reqfunc" jsclassname="jsobj" />

<script language="JavaScript"> 

function checkRequired() {
    var testVal = document.getElementById('myField').value;
    return testAjax(testVal);
    /* more processing that should stop if ajaxCallBack returns false */
}
function testAjax(testVal) {
    var instance = new jsobj();
    instance.setCallbackHandler(ajaxCallBack);
    instance.checkProfile(testVal);
}

function ajaxCallBack(returns) { 
    alert(returns); 
// returns correctly "true" if value checks against db, "false" if it doesn't
// HOW DO I PASS THIS VALUE BACK TO checkRequired ???
}
</script>

<form>
<input type="text" name="myField" id="myField" value=""><p>
<input type="button" value="Check with Ajax" onClick="return checkRequired()">
</form>

many thanks

Jeff Mercado
  • 129,526
  • 32
  • 251
  • 272
ion
  • 51
  • 1
  • 7

2 Answers2

1

Unless you build your main function to 'wait' for the return, you can't return your result to that instance of the function; it has already exited, so to speak. Using cfajax it is probably possible to tweak the main function to call and wait, but the simple solution is to have the callback subsequently recall your main function and treat the existence of the result/return as the flag as to whether to process or call the ajax.

function checkRequired(return) {
  if(return != null) {
    /* more processing */
  } else {
    testAjax(testVal);
  }
}

function ajaxCB(return) {
  checkRequired(return);
}

I would probably refactor a bit more but you should get the idea.

williambq
  • 1,125
  • 7
  • 12
  • i've collapsed the 3 functions into a single one, but it's still the same thing, the inner func doesn't pass "return false" to the main one, the form will still "submit" regardless of the value of "returns"function ~~~~ function checkRequired() { var testVal = document.getElementById('myField').value; var instance = new jsobj(); var r = instance.setCallbackHandler( function(returns) { if(returns == 0) {return false;} } ); instance.checkProfile(testVal); document.getElementById('testForm').submit(); } – ion Mar 19 '14 at 15:01
  • thanks william, yours works too, plus it makes any new vars available to the function. still have to put it at the very top of the function ~~~~~ function checkRequired(r) { if (typeof r != 'undefined') { if (r == 1){ document.getElementById('testForm').submit(); } else {alert("Something wrong"); return false;} } else { var testVal = document.getElementById('myField').value; testAjax(testVal); } } function testAjax(testVal) { var instance = new jsobj(); instance.setCallbackHandler(ajaxCallBack); instance.checkProfile(testVal); } function ajaxCallBack(returns) { checkRequired(returns); } – ion Mar 19 '14 at 16:21
0

it's really kind of a hack, not what i was looking for, but for what it's worth: if i put this stanza at the very end of my function, with the callBack collapsed within the main function, it would work

function checkRequired() {
  var testVal = document.getElementById('myField').value;
  var instance = new jsobj();
  var r = instance.setCallbackHandler(
    function(returns) {
      if(returns == 1)  {
        document.getElementById('testForm').submit();
      } else { alert("Something wrong"); }
    }
  );
  instance.checkProfile(testVal);
}
ion
  • 51
  • 1
  • 7