How to insert data in table using serialize ajax?

How to use serialize function in ajax?

Database Table

File Name : table

CREATE TABLE `user` (
`id` INT( 3 ) NOT NULL AUTO_INCREMENT ,
`username` VARCHAR( 30 ) NOT NULL ,
`password` VARCHAR( 50 ) NOT NULL ,
`email` VARCHAR( 50 ) NOT NULL ,
PRIMARY KEY ( `id` )
) ENGINE = INNODB;


When submit button is clicked, ajax function will be called with method as POST, url as ajax.php and sucess message.

Example

File Name : index.php

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#submit").click(function(){
var form2=$("#form1").serialize();
$.ajax({
type: "POST",
url: "data.php",
data: form2,
success: function(html)
{
$("#load").html(html);
}
});
return false;
});
});
</script>
</head>
<body>
<form method="post" action="" id="form1">
Username: <input type="text" name="name" id="name" /><br />
Age:<input type="text" name="age" id="age"/><br />
<input type="submit" name="submit" id="submit" /><br />
<div id="load"></div>
</form>
</body>
</html>

server code

File Name : data.php

<?php
if(isset($_POST['submit']))
{
$name=$_POST['name'];
$age=$_POST['age'];

echo $name;
echo $age;
}
?>





Previous Next


Trending Tutorials




Review & Rating

0.0 / 5

0 Review

5
(0)

4
(0)

3
(0)

2
(0)

1
(0)

Write Review Here