3

Please I am trying to simultaneously submit and validate my form to my database through the use of Ajax, but it is not working for me. Here is my jquery

$(document).ready(function(){
       $(".button").click(function(){
             $("#myform").validate();

            //Ajax to process the form
       $.ajax({
           type: "POST",
           url: "process.php",
           data: { firstname: $("#firstname").val()},
           success: function(){
                               $('#message').html(data);
                               }
               });
          return false;
       });
      });

The problem is when I submit the form,the Ajax form submit to itself. Please What is the right way to use the jquery validate and $.ajax together?

0

4 Answers 4

5

Pass data as a parameter in your success function:

success: function(data){

Your success function won't do anything because you haven't defined data

Sign up to request clarification or add additional context in comments.

Comments

3

Try this (working for me as expected):

HTML Form:

<link rel="stylesheet" href="http://jquery.bassistance.de/validate/demo/css/screen.css" />
<script src="http://jquery.bassistance.de/validate/lib/jquery.js"></script>
<script src="http://jquery.bassistance.de/validate/jquery.validate.js"></script>
<script>    
// JQuery Script to submit Form
$(document).ready(function () {
    $("#commentForm").validate({
        submitHandler : function () {
            // your function if, validate is success
            $.ajax({
                type : "POST",
                url : "process.php",
                data : $('#commentForm').serialize(),
                success : function (data) {
                    $('#message').html(data);
                }
            });
        }
    });
});
</script>

<form class="cmxform" id="commentForm" method="get" action="">
    <fieldset>        
        <p>
            <label for="cname">Name (required, at least 2 characters)</label>
            <input id="cname" name="name" minlength="2" type="text" required />
            <p>
                <label for="cemail">E-Mail (required)</label>
                <input id="cemail" type="email" name="email" required />
            </p>
            <p>
                <label for="curl">URL (optional)</label>
                <input id="curl" type="url" name="url" />
            </p>
            <p>
                <label for="ccomment">Your comment (required)</label>
                <textarea id="ccomment" name="comment" required></textarea>
            </p>
            <p>
                <input class="submit" type="submit" value="Submit" />
            </p>
    </fieldset>
</form>

<div id="message"></div>

PHP Code:

<?php
echo $_POST['email'];
?>

7 Comments

it just validates the form but cannot submit the form
@user2418765: check my new complete code and try to find what is wrong in your code.
thanks once more.I have really spend so many hours just to get this done
PLEASE I HAVE BEEN BANNED FROM THIS SITE, CAN YOU PLEASE ASSIST ME OUT OF THE BAN..THANKS
@Duahs: I didn't get you, what are you trying to ask..?
|
3

You forget to pass the response

$(document).ready(function() {
    $(".button").click(function() {

        //check the validation like this
        if ($("#myform").valid()) {
            //Ajax to process the form
            $.ajax({
                type: "POST",
                url: "process.php",
                data: {
                    firstname: $("#firstname").val()
                },

                //you forget to passs the response
                success: function(response) {
                    $('#message').html(response);
                }
            });
            return false;
        }
    });
});

5 Comments

$("#myform").validate() this line is updated and add full http url url: "process.php", check the validate rules also, if everything is fine then it'll submit
,this only validates the form but cannot submit the data
sorry,im facing the same problem
hi Sundar your answer is working, but you would double click on the submit button before it could submit.
0

First of all, why would you submit form if validation is not passed? Try this, if validate really validates:

$(function(){
    $(".button").click(function(){
        var myform = $("#myform");
        if (myform.validate()) {        
            $.post("process.php", myform.serialize(), function(data){
                $('#message').html(data);
            });            
        }
        return false;
    });     
});

1 Comment

the validation works but the submission does not work,it only submits to itself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.