Jump to content

Preventing page refresh after submit


Niixie

Recommended Posts

Hey people on phpfreaks!

 

On my website, I'm trying to get to know javascript. So, i made this code where its supposed to do a small validation on the entered data, and when I press the submit button, a alert box appears, BUT, when i close that alert box, the fields resets?...

 

I tryed to prevent this with

event.preventDefault

 

both before and after the alert box?

 

CODE:

 

var name = document.getElementById('name-field');
    var email = document.getElementById('email-field');
    var send = document.getElementById('send-field');
    
    if(!name.value==""){
        if(!email.value==""){
            if(emailValidator(email, "Indtast en gyldig emailaddresse!")){
                alert("Tak for din email "+name.value+"!\n\nDin email vil blive læst så hurtigt som muligt af vores staff.");
            }
        }else{
            alert("Email feltet må ikke være tomt!");
            email.focus();
        }
    }else{
        alert("Navn feltet må ikke være tomt!");
        name.focus();
    }

Link to comment
https://forums.phpfreaks.com/topic/251073-preventing-page-refresh-after-submit/
Share on other sites

Thank you, but it didn't work?

 

Current code:

if(!name.value==""){
        if(!email.value==""){
            if(emailValidator(email, "Indtast en gyldig emailaddresse!")){
                alert("Tak for din email "+name.value+"!\n\nDin email vil blive læst så hurtigt som muligt af vores staff.");
            }
        }else{
            alert("Email feltet må ikke være tomt!");
            email.focus();
            return false;
        }
    }else{
        alert("Navn feltet må ikke være tomt!");
        name.focus();
        return false;
    }

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.