G_Man
G_Man

Reputation: 1343

Basic jQuery and PHP Ajax Form Failing

I am stumped I am new to jQuery and trying to figure out what is wrong with my basic ajax attempt. Here is the form.

<div id="results"></div>
<form id="myform" name="myform" action="" method="post">
<p>
<label for="email">Email</label>
<input type="text" name="email"  id="email" class="sf" />
</p>
<p>
<input type="submit" name="submit" class="submit" />
</p>
</form>

And here is the jQuery Code

jQuery(document).ready(function(){
    jQuery("#myform").validate({
        rules: {
        email: {
            required: true,
            email: true,
        }
    },
    messages: {
        email: "Please enter a valid email address"
    },
        submitHandler: function(form) {
            event.preventDefault();
            $.post('/ajax/emailadd.php', $("#myform").serialize(), function(data) {
                $('#results').html(data);
            });
        }
    });
});

The validation is working great but I can never get the Ajax post to fire according to FireBug and the PHP file just has a simple echo statement.

Upvotes: 2

Views: 177

Answers (2)

jlrvpuma
jlrvpuma

Reputation: 271

I think:

 submitHandler: function(event) {
        event.preventDefault();
        $.post('/ajax/emailadd.php', $("#myform").serialize(), function(data) {
            $('#results').html(data);
        });

Note argument 'event'.

Upvotes: 1

brenjt
brenjt

Reputation: 16297

Try removing the event.preventDefault(); There is not event object anywhere so it is probably erroring out. Try checking your error console.

Upvotes: 1

Related Questions