0

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.

Edited by daniel36

2
Contributors
1
Reply
3
Views
4 Years
Discussion Span
Last Post by GliderPilot
0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.