I am trying to insert input values into database with the help of form without refreshing the page.For this my form is

<html>
  <head>
<title>Jquery test</title>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$('insert').click(function() {

    $.post("test2.php", $(this).closest("form").serialize());
});
</script>
  </head>
   <body>
<form id="test">
<table>
<tr>
<td>First Name</td>
<td><input type="text" name="fname"></td>
</tr>
<tr>
<td>Last Name</td>
<td><input type="text" name="lname"></td>
</tr>
<tr>
<td colspan="2"><input type="button" name="insert" value="insert"></td>
</tr>
</table>
</form>
   </body>
</html>

And my test2.php file is-

<?php 
mysql_connect('localhost','root','');
mysql_select_db('test');
$sql="INSERT INTO user (first_name,last_name) VALUES ('".$_POST['fname']."','".$_POST['lname']."')";
mysql_query($sql);
?>

but this is not working for me.Please help me.Thank you in advance.

Add an or die(mysql_error()) to your query to see if it's failing. It's a very bad idea to add un-verified data to your DB. At the absolute very least you should make sure your POST data is set. Without any form of error handling it's difficult to say where exactly this is failing.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.