Javascript - simple submit button issue

Hello guys,

I have this javascript code:
$("#submit").click(function() { 
        //get input field values 
        var newsletter_email      = $('input[name=newsletter_email]').val();
        
        //simple validation at client's end
        //we simply change border color to red if empty field using .css()
        var validation = true;
       
        if(newsletter_email==""){ 
            $('input[name=newsletter_email]').css('border-color','red'); 
            validation = false;
        }
       
        //everything looks good! proceed...
        if(validation) 
        {
            //data to be sent to server
            post_data = {'userEmail':newsletter_email};
            
            //Ajax post data to server
            $.post('ajaxcontact2.php', post_data, function(response){  

                //load json data from server and output message     
                if(response.type == 'error')
                {
                    output = '<div class="error">'+response.text+'</div>';
                }else{
                    output = '<div class="success">'+response.text+'</div>';
                    
                    //reset values in all input fields
                    $('#potvrdit input[type=newsletter-email]').val(''); 
                }
                
                
            }, 'json');
            
        }
    });

Open in new window


When I click on submit button and left the email field empty, it doesn't set the border-color: red as it should. Instead it reaload the page with this URL - site.com/?newsletter_email=

So it doesn't even get to the PHP file, but I guess I have to have something wrong in the javascript. Please help.

NOTE: It's already in the document ready function.
LVL 1
Fajer39Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris StanyonWebDevCommented:
You need to prevent the form from submitting the normal way so that it only submits via AJAX. Change your opening code to this:

$("#submit").click(function(e) {
    e.preventDefault();
    ...

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Fajer39Author Commented:
Thank you.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
AJAX

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.