0

hi i am new enough to JavaScript and am wonder how to send details in email after validation ,heres my code

<script type="text/javascript"> 

 var compName=false;
 var compContry=false;
 var compsub=false;
 var compphone=false;
 var compemail=false;


function validate_form(form)
{
    if(compName)
    {
        document.getElementById('country').focus();
        compName=true;
        if(compContry)
        {
            document.getElementById('subject').focus();
            compContry=true;
            if(compsub)
            {
                document.getElementById('Phone').focus();
                compsub=true;
                if(compphone)
                {
                    document.getElementById('email').focus();
                    compphone=true;
                    if(compemail)
                    {

                        //I just use alert to show it works. 
                        alert("Your Details Are Sent ");
                        compemail=true;
                    }
                    else
                    {
                        document.getElementById('email').focus();
                        compemail=false;
                    }
                }
                else
                {
                    document.getElementById('Phone').focus();
                    compphone=false;
                }
            }
            else
            {
                document.getElementById('subject').focus();
                compsub=false;
            }
        }
        else
        {
            document.getElementById('country').focus();
            compContry=false;
        }
    }
    else
    {
        document.getElementById('username').focus();
        compName=false;
    }

}
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
martin
  • 1
  • 1
  • Use your server side code to do it. – alex May 25 '11 at 07:32
  • possible duplicate of [Send emal using Javascript.](http://stackoverflow.com/questions/3296402/send-emal-using-javascript) and [others](http://stackoverflow.com/search?q=javascript+send+email) – Felix Kling May 25 '11 at 07:34

3 Answers3

0

you can add return false; after each validation if you are using cutom js for example

var first_name = $("#first_name");
        if(!$.trim(first_name.val()).length) {
            first_name.closest('.form-group').removeClass('has-success').addClass('has-error');
            return false;
        }else{
            first_name.closest('.form-group').removeClass('has-error').addClass('has-success'); 
        }

return false will stop the submit button to proceed until the form is not filled.

freelancer

0

You need some kind of server-side scripting to send emails.

Thor Jacobsen
  • 8,621
  • 2
  • 27
  • 26
0

After it passes validation you can submit your form like this

document.formname.submit();

in your case, this goes at the end of your function

if (comP... == true) {
   form.submit();
}
Ibu
  • 42,752
  • 13
  • 76
  • 103
  • do i not need to put in the email address to where i'm sending it ? – martin May 25 '11 at 07:46
  • that goes in your html form tag `
    ` the code to send the email will be handled by your server, javascript will only do the first layer of validation
    – Ibu May 25 '11 at 07:49
  • @martin, see the other answers that say you need to send mail server-side. The closest you can come to sending mail via javascript would be to use the HTML "mailto" command. You could use it in the "action" attribute of the
    tag. So, the submittal of a form like this
    will attempt to open the email client on the user's machine to allow them to send an email. You can control form submittal as indicated by Ibu above.
    – squidbe May 25 '11 at 08:00
  • @lbu, hi i never usedphp before and am going to give it a try , i found this Script http://www.freecontactform.com/email_form.php and am just wondering can i put the php in the head of my page like JavaScript – martin May 25 '11 at 08:05
  • @martin, ok since you havent used php [this is all](http://devzone.zend.com/article/627-PHP-101-PHP-For-the-Absolute-Beginner) i am going to give you and you are going to come back a pro – Ibu May 25 '11 at 08:10