ilyes kooli
ilyes kooli

Reputation: 12043

Make form use ajax

I am having issue making a form be sent using ajax.

Here is my code:

<form id="form" method="get">
    <input type="text" name="name" id="name"><br>
    <input type="text" name="email" name="email"><br>
    <input type="submit" id="submit" value="submit">
</form>


$('#submit').click(function(event){
    alert('ajax');
});

The "ajax" alert shows, but then the page gets reloaded! How can I stop this behaviour?

Upvotes: 0

Views: 411

Answers (2)

ahmet2106
ahmet2106

Reputation: 5007

use $.submit() instead of click :)

$('#form').submit(function(event){
    alert('ajax');
    // here some ajax functions for sending via get or post ;)
    return false; // this stops loading the action site.. its something like e.preventDefault() on links
});

Upvotes: 2

ilyes kooli
ilyes kooli

Reputation: 12043

You have to use preventDefault

$('#submit').click(function(event){
    event.preventDefault();
    alert('ajax');        
});

Upvotes: 3

Related Questions