I want to use a cloned div that contains a form to insert a record to the database using this code
$(document).ready(function() {
$("#button").click(function() {
$("<div class='newclone' id='xxx'><article><form method='post' id='aj'><input type='checkbox'><label>Firstname</label><input type='text' value='' name='firstname'><label>Secondname</label><input type='text' value='' name='secondname'><label>City</label><input type='text' value='' name='city'><input type='hidden' value='4'></article><input type='submit' value='insert' class='one'><button class='two'>Delete</button><button class='three'>Cancel</button></form></div>").appendTo('.clone-container');
});
$('.one').click(function() {
$.ajax({
type: "POST",
url: "insert.php",
data: $("#aj").serialize(),
success: function() {
alert('Inserted!');
}
});
});
});
This is the php file
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("clone", $con);
$firstname=$_POST['firstname'];
$secondname=$_POST['secondname'];
$city=$_POST['city'];
mysql_query("INSERT INTO clone(firstname,secondname,city) VALUES ('$firstname','$secondname','$city')");
edit
the problem is that there seem to be nothing posted
Test each part separately. Is the Ajax request sending the right data? Is the PHP script even getting the data? (Try
echo <pre>;print_r($_POST);exit;.) Is yourINSERTquery being formulated correctly? Was it successful?You don’t yet know exactly what your problem is. First figure out what the problem is. Then it will probably be easy to figure out how to solve it.