Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I call a jquery function on submitting a form?

Tags:

jquery

ajax

forms

I have a form which is given below :

 <form method="post" id="contactForm" action="">
                <fieldset class="contactFieldset">
                    <ul>
                        <li>
                            <label for="contactName" class="leftLabel">*Name:</label>
                            <input type="text" name="contactName" id="contactName" class="contactInput required" value="" />

                        </li>
                        <p></p>
                        <li>
                            <label for="email" class="leftLabel">*Email:</label>
                            <input type="text" id="email" name="email" class="contactInput email required" value="" />
                        </li>
                      <span class="simple-success">I'll be in  touch soon</span>
                        <li>
                            <label for="subject" class="leftLabel">*Subject:</label>
                            <input type="text" name="subject" id="subject" class="contactInput required" value="" />
                        </li>
                        <p></p>
                        <li>
                            <label for="message" class="leftLabel">Message:</label>
                            <textarea rows="10" cols="40" id="message" name="message" class="contactTextarea required"></textarea>

                        </li>
                       <p></p>
                        <li>


                            <input type="image" src="images/submitbutton.png" alt="Submit button" name="submit_button" class="submit_button" id="submit_button">

                        </li>
                    </ul>
                </fieldset>
            </form>

I am trying to extract the data from this form via jquery so that I can pass it on to the php file and send an email but the jquery function is not being called on clicking the button.The code is given below:

    $(function() {
  $('.error').hide();
  $('.simple-sucess').hide();

  $(".submit_button").click(function() {





        /*get the email value*/

        var email = $("input#email").val();
        var name = $("input#contactName").val();
        var subject = $("input#subject").val();
        var message=$("input#message").val();
    //  alert("email"+email);

/* Check if the email is good or bad */

var goodEmail = email.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\.info)|(\.sex)|(\.biz)|(\.aero)|(\.coop)|(\.museum)|(\.name)|(\.pro)|(\.arpa)|(\.asia)|(\.cat)|(\.int)|(\.jobs)|(\.tel)|(\.travel)|(\.xxx)|(\..{2,2}))$)\b/gi);
    apos=email.indexOf("@");dotpos = email.lastIndexOf(".");lastpos=email.length-1;
    var badEmail    = (apos<1 || dotpos-apos<2 || lastpos-dotpos<2);

/*If the email is bad ,display the error message*/

if (email=="" || !goodEmail || badEmail) {

        $("email").focus();
      return false;
    }


        var dataString = 'email='+ email + '\n Name='+ name+ '\n Subject='+ subject+ '\n message='+ message;
        alert (dataString);

        $.ajax({
      type: "POST",
      url: "mai.php",
      data: dataString,
      success: function() {
        $('.simple-sucess').fadeIn(100).show();
        $('.contact_form').fadeOut(100).hide();
        $('.simple_error').fadeOut(100).hide();


      }
     });
    return false;
    });
});

Any ideas on what I might be doing wrong ?

Thanks

like image 844
Mervin Avatar asked Jan 25 '12 07:01

Mervin


1 Answers

Use onsubmit for checking of a form is submitted:

$('#contactForm').on('submit', function() {
  alert('You submitted the form!');
});
like image 101
Wouter J Avatar answered Sep 30 '22 08:09

Wouter J