I have that form. but it dont work.... cant save new values.
index.php
<html>
<head>
<title>Silmaring</title>
<script src="js/jquery.js"></script>
<script src="js/script.js"></script>
<script src="js/jquery-ui-1.8.17.custom.min.js"></script>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<br>
<div style="margin-left: 20%;margin-top: 5%;">
<input type="button" value="Lisa" id="add_new"><p>
<table width="70%" border="0" cellpadding="0" cellspacing="0" class="table-list">
<tr>
<th width="20%">Kooli nimi</th>
<th width="40%">Kustuta</th>
</tr>
</table>
</div>
<div class="entry-form">
<form name="userinfo" id="userinfo">
<table width="100%" border="0" cellpadding="4" cellspacing="0">
<tr>
<td colspan="2" align="right"><a href="#" id="close">Sulge</a></td>
</tr>
<tr>
<td>Kooli nimi</td>
<td><input type="text" name="kool"></td>
</tr>
<tr>
<td align="right"></td>
<td><input type="button" value="Salvesta" id="save"><input type="button" value="Tühista" id="cancel"></td>
</tr>
</table>
</form>
</div>
</body>
</html>
ajax.php
<?
error_reporting(E_ALL);
ini_set('display_errors', '1');
$conn = mysql_connect("localhost","user","pass") or die(mysql_error());
mysql_select_db("silmaring",$conn) or die(mysql_error());
if(isset($_POST) && count($_POST)){
$kool = $_POST['kool'];
$item_id = $_POST['$item_id'];
$action = $_POST['action'];
if($action == "save"){
$res = mysql_query("insert into kool values('','".$kool."')");
if($res){
echo json_encode(
array(
"success" => "1",
"row_id" => time(),
"kool" => htmlentities($kool),
)
);
}
}
else if($action == "delete"){
$res = mysql_query("delete from info where id = $");
if($res){
echo json_encode(
array(
"success" => "1",
"item_id" => $item_id
)
);
}
}
}else{
echo "No data set";
}
?>
script.js
$(document).ready(function(){
$("#save").click(function(){
ajax("save");
});
$("#add_new").click(function(){
$(".entry-form").fadeIn("fast");
});
$("#close").click(function(){
$(".entry-form").fadeOut("fast");
});
$("#cancel").click(function(){
$(".entry-form").fadeOut("fast");
});
$(".del").live("click",function(){
ajax("delete",$(this).attr("id"));
});
function ajax(action,id){
if(action =="save")
data = $("#userinfo").serialize()+"&action="+action;
else if(action == "delete"){
data = "action="+action+"&item_id="+id;
}
$.ajax({
type: "POST",
url: "ajax.php",
data : data,
dataType: "json",
success: function(response){
if(response.success == "1"){
if(action == "save"){
$(".entry-form").fadeOut("fast",function(){
$(".table-list").append("<tr><td>"+response.kool+"</td><td><a href='#' id='"+response.row_id+"' class='del'>Kustuta</a></a></td></tr>");
$(".table-list tr:last").effect("highlight", {
color: '#4BADF5'
}, 1000);
});
}else if(action == "delete"){
var row_id = response.item_id;
$("a[id='"+row_id+"']").closest("tr").effect("highlight", {
color: '#4BADF5'
}, 1000);
$("a[id='"+row_id+"']").closest("tr").fadeOut();
}
}else{
alert(response.msg);
}
},
error: function(res){
alert("Unexpected error! Try again.");
}
});
}
});