简体   繁体   中英

Form doesn't return true and won't send to PHP

So the validation for the form works, but I cannot get it to send to the php file. I'm assuming it has something to do with the return false/true and the end.

function validateForm(contact) {
    var name = document.getElementById('name').value
    var email = document.getElementById('email').value
    var msg = document.getElementById('message').value

    if (name == '') 
        {
        $('.nameerror').html('Please provide your name').fadeIn(1000);      

        }else if
        (!validateName(name)) {
            $('.nameerror').html('Only letters and spaces are allowed').fadeIn(1000);
        }


    if (email == '') 
        {
        $('.emailerror').html('Please provide your email').fadeIn(1000);    
        }else if
        (!validateEmail(email)) {
            $('.emailerror').html('Invalid email format').fadeIn(1000);     
        }



    if (msg == '') 
        {
        $('.msgerror').html('What can we help you with?').fadeIn(1000); 
        }


return false;

if($.trim($('.nameerror').text()) == ''){
    return true;    
} 

};

I think your last section of code should read like this:

if($.trim($('.nameerror').text()) == '')
{
    // You can do stuff here first if everything is good.
    return true;    
}
else
{
    // Or you can do stuff here for a failed submission.
    return false;
}

You are exiting the function before the last if statement is checked.

You must use this code:

function validateForm(contact) {
    var name = document.getElementById('name').value
    var email = document.getElementById('email').value
    var msg = document.getElementById('message').value

    if (name == '') {
        {
        $('.nameerror').html('Please provide your name').fadeIn(1000);      

        }else if
        (!validateName(name)) {
            $('.nameerror').html('Only letters and spaces are allowed').fadeIn(1000);
        }
        return false;
}


    if (email == '') {
        {
        $('.emailerror').html('Please provide your email').fadeIn(1000);    
        }else if
        (!validateEmail(email)) {
            $('.emailerror').html('Invalid email format').fadeIn(1000);     
        }
    return false;
}

    if (msg == '') {
        $('.msgerror').html('What can we help you with?').fadeIn(1000); 
        return false;
}  

if($.trim($('.nameerror').text()) == ''){
    return true;    
} 

};

Instead of checking to see if a particular element has html in it... why don't you just set a flag? This makes everything a bit more simplistic.


function validateForm(contact) {
    var name = document.getElementById('name').value
    var email = document.getElementById('email').value
    var msg = document.getElementById('message').value
    var flag = true;

    //do this for each of your if statements
    if(there is an error case) {
        //do whatever you want to the DOM
        flag = false;
    }
return flag;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM