How do I cancel a form submission?

ganeshbala
Kilo Contributor

I would like to cancel the form submission in case the "if" statement below is true. how can I acheive that?? I added "return false" inside the if statement but its not working. kindly do help me out..

function onSubmit() {

    //Type appropriate comment here, and begin script below

  var t = g_form.getValue('u_date');

  var a = new GlideAjax('testscript');

  a.addParam('sysparm_name','myfunction');

  a.addParam('sysusername', 'bala');

  a.getXML(myback);

  function myback(res)

  {

  var answer= res.responseXML.documentElement.getAttribute("answer");

  if(t!=answer)

  {

  alert("current date is" + answer);

  }

}

   

}

8 REPLIES 8

oharel
Kilo Sage

In short (continue alikuttyka 's answer):


function onSubmit() {


  //Type appropriate comment here, and begin script below


  var t = g_form.getValue('u_date');


if(your condition) {


alert('some alert');


return false;


}


}



harel


amlanpal
Kilo Sage

Hi Bala,



In such cases you can use synchronous GlideAJAX and use getXMLWait() instead of getXML(). Please find the link Examples of synchronous GlideAjax . In this case if you are using getXMLWait(), you do not have to use callback function. Please find the code below:



function onSubmit() {


    //Type appropriate comment here, and begin script below


  var t = g_form.getValue('u_date');


  var a = new GlideAjax('testscript');


  a.addParam('sysparm_name','myfunction');


  a.addParam('sysusername', 'bala');


  a.getXMLWait();


  var answer= a.getAnswer();


  if(t!=answer)


  {


  alert("current date is" + answer);


  }  


}



I hope this helps.Please mark correct/helpful based on impact


Just a note to consider:


ServiceNow does not recommend using asynchronous getXMLWait();


In the link above, it is stated so:


If your use case demands that no further processing can occur until the GlideAjax response has been received, you can use getXMLWait(). However, because this will slow down your code and lock the user session until the response is received, it is generally recommended that you use getXML() with a callback function.


Prasad43
Tera Guru

Hi,

You need to use synchronous glide ajax here.

 

https://docs.servicenow.com/bundle/rome-application-development/page/script/ajax/topic/p_AJAX.html

 

var ga = new GlideAjax('HelloWorld') ;
ga.addParam('sysparm_name','helloWorld');
ga.addParam('sysparm_user_name',"Bob");
ga.getXMLWait(); 
alert(ga.getAnswer());

var HelloWorld = Class.create();
HelloWorld.prototype = Object.extendsObject(AbstractAjaxProcessor, {
helloWorld: function() 
 { return "The Server Says Hello " + this.getParameter('sysparm_user_name') + "!"; } } );