Brad Hazelnut
Brad Hazelnut

Reputation: 1621

jquery popup after form submission

I have the following code.

<script type="text/javascript">
        $(document).ready(function () {
            $("#submit").click(function (e) {
                e.preventDefault();

               /* Get some values from elements on the page: */
                var values = $(this).serialize();
                $.ajax({
                    url: "submit.php",
                    type: "post",
                    data: values            
                });
                $("#dialog").dialog({modal: true, height: 200, width: 300 });



            });
        });
    </script>


<body>
<form id="contactfrm">
<input type="text" name="fullname"/>
<input type="text" name="email"/>
<input type="text" name="phone"/>
<a href="" class="submit" id="submit">submit</a>
</form> 
</body>

the only reason i am using a link is because a designer has designed the link button and wasn't able to do a submit button.

Now when i click the button it popsup the window but it never submits the form. I can't understand why this is happening?

Upvotes: 0

Views: 2872

Answers (3)

bares43
bares43

Reputation: 21

I think you are getting the values in a wrong way. You should use

 var values = $(this).parent().serialize();

instead of

var values = $(this).serialize();

But what about use form button with CSS?

input[type=submit] {
    background: none;
    border: none;
    cursor: Pointer;    
}

Upvotes: 0

Jonan
Jonan

Reputation: 2536

If you want code to run when the ajax request is done, set the succes parameter:

$.ajax({
    url: "submit.php",
    type: "post",
    data: values,
    success: function(data){
        $("#dialog").dialog({modal: true, height: 200, width: 300 });
    }
});

If you just put the code behind the ajax request, it will immediately run after the request has started instead of when it has succeeded.

Upvotes: 1

EduardoFernandes
EduardoFernandes

Reputation: 3449

This is happening because you use e.preventDefault(); which prevents your form to submit. In order to submit it, put in the end of your code: $( "form" ).submit();.

When you call the $.ajax(...) method an asynchronous call will be made, so the form will not be submitted.

Moreover, note that when you call $("#dialog").dialog({modal: true, height: 200, width: 300 });, it will be closed after the $( "form" ).submit(); is called. The thing is, I do not understand the reason for opening a dialog when the form is to be submitted.

Upvotes: 2

Related Questions