1 <!DOCTYPE html>
2 <html>
3 <head>
4 <title>Webslesson Tutorial | Jquery Post Form Data using Ajax serialize() method</title>
5 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
6 <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
7 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
8 </head>
9 <body>
10 <br /><br />
11 <div class="container" style="width:600px;">
12 <h3>Jquery Post Form Data using Ajax serialize() method</h3><br />
13 <form id="submit_form">
14 <label>Name</label>
15 <input type="text" name="name" id="name" class="form-control" />
16 <br />
17 <label>Message</label>
18 <textarea name="message" id="message" class="form-control"></textarea>
19 <br />
20 <input type="button" name="submit" id="submit" class="btn btn-info" value="Submit" />
21 </form>
22 <div id="response"></div>
23 </div>
24 </body>
25 </html>
26 <script>
27 $(document).ready(function(){
28 $('#submit').click(function(){
29 var name = $('#name').val();
30 var message = $('#message').val();
31 if(name == '' || message == '')
32 {
33 $('#response').html('<span class="text-danger">All Fields are required</span>');
34 }
35 else
36 {
37 $.ajax({
38 url:"insert.php",
39 method:"POST",
40 data:$('#submit_form').serialize(),
41 beforeSend:function(){
42 $('#response').html('<span class="text-info">Loading response...</span>');
43 },
44 success:function(data){
45 $('form').trigger("reset");
46 $('#response').fadeIn().html(data);
47 setTimeout(function(){
48 $('#response').fadeOut("slow");
49 }, 5000);
50 }
51 });
52 }
53 });
54 });
55 </script>
56