Search This Blog

Saturday 24 March 2018

Insert data through ajax into MySql database.

1. Create form.php and copy below code into file.

<script type="text/javascript">

$(document).ready(function(){
$('#submit').click(function(){
var name= $('#name').val();
var email= $('#email').val();
var sdatatring='name1='+ name +'&email1='+ email;
$.ajax({
type:"POST",
  url:"insert.php",
  data:sdatatring,
  cache: false,
  success:function(result){
  alert(result);

  }});


});

});
</script>
  <form method="post" action="" name="frm">
  Name:<input type="text" name="name" id="name" value=""><br>
  Email:<input type="text" name="email" id="email" value=""><br>
  <input type="button" name="submt" id="submit" value="submit" />
 
  </form>

 2. Create insert.php and copy below code into file.
 <?php
print_r($_POST);
$con=mysql_connect("localhost","root","");
mysql_select_db('dbname');
mysql_query('insert into tablename('colname')values(value)');
 ?>
Note:You need to include jQuery  library in form.php.

1 comment: