Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Page re-load/refresh after JavaScript alert - dont want it do!

Tags:

javascript

My JavaScript function is working but for some reason after the alert is displayed inside my IF statement the page re-loads/refresh and I do not want it to. Why is this and how can I change my function so that it will not do this?

My function

function valSubmit(){    
    varName = document.form1.txtName.value;
    varSurname = document.form1.txtSurname.value;
    varEmail = document.form1.txtEmail.value;
    varOrg = document.form1.txtOrg.value;

    if (varName == "" || varSurname == "" || varEmail == "" || varOrg == "" ) 
    {

     alert("Please fill in all mandatory fields");  

    }
    else
    { 
        document.body.style.cursor = 'wait';
        document.form1.btnSubmit.style.cursor = 'wait';
        document.form1.action = "http://now.eloqua.com/e/f2.aspx"
        document.form1.submit();
        return true;    
    }

 }

p.s I am using ASP.NET 3.5

like image 854
Etienne Avatar asked Oct 22 '25 16:10

Etienne


2 Answers

Here is your complete function with the return false statement added.

Additionally, when you call valSubmit, it should look like this:

... onsubmit="return valSubmit();"...

Note, you need to specify return here also.

Here is the function:

function valSubmit(){

varName = document.form1.txtName.value;
varSurname = document.form1.txtSurname.value;
varEmail = document.form1.txtEmail.value;
varOrg = document.form1.txtOrg.value;

 if (varName == "" || varSurname == "" || varEmail == "" || varOrg == "" ) 
 {

     alert("Please fill in all mandatory fields");
     return false;

 }
 else
 { 
    document.body.style.cursor = 'wait';
    document.form1.btnSubmit.style.cursor = 'wait';
    document.form1.action = "http://now.eloqua.com/e/f2.aspx"
    document.form1.submit();
    return true;    
 }

}
like image 127
Fenton Avatar answered Oct 25 '25 05:10

Fenton


you have to add return false; to stop the default action from taking place from the form submit

like image 35
Erik Avatar answered Oct 25 '25 04:10

Erik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!