I've created a live table to show, edit and delete data from my database using Ajax, PHP, and MySQL. The problem is that my PHP code isn't executed and I have this error
Undefined index: id_clasa in <b>E:\xampp2\htdocs\Licenta (v.final)\admin\clase_zbor\delete.php</b> on line <b>5</b><br />
Date sterse!
The id_clasa
is the name of one of the columns in my database.
My delete.php file
<?php
require('../../connect.php');
$sql="DELETE FROM clasa WHERE id_clasa='".$_POST['id_clasa']."'";
if(mysqli_query($conn,$sql)){
echo 'Date sterse!';
}
?>
The script that displays the data from the database in a table
<?php
require('../../connect.php');
$output='';
$sql="select * from clasa order by id_clasa";
$result=mysqli_query($conn,$sql);
$result_check=mysqli_num_rows($result);
$output.='
<div class="table-responsive">
<table class="table table-striped text-center table-bordered">
<thead class="thead-dark">
<tr>
<th>ID</th>
<th>Nume_clasa</th>
<th>Action</th>
</tr>
</thead>';
if($result_check > 0){
while($row=mysqli_fetch_array($result)){
$output.='<tr>
<td>'.$row["id_clasa"].'</td>
<td class="nume_clasa" data-id1="'.$row["id_clasa"].'" contenteditable>'.$row["nume_clasa"].'</td>
<td><button type="button" name="delete_btn" data_id2="'.$row["id_clasa"].'" class="btn btn-danger btn_delete" >Delete</button></td>
</tr>
';
}
$output.='<tr>
<td></td>
<td id="nume_clasa" contenteditable></td>
<td><button class="btn btn-success" name="btn_add" id="btn_add" class="btn btn-xs btn-succes">Add</button></td>
</tr>';
}
else{
$output.='<tr>
<td colspan="3">Nu exista date</td>
</tr>';
}
$output.='</table>
</div>';
echo $output;
?>
the ajax script(which is in flight_class.php)
<script>
$(document).ready(function(){
function fetch_data()
{
$.ajax({
url:"select.php",
method:"POST",
success:function(data){
$('#live_data').html(data);
}
});
}
fetch_data();
$(document).on('click','#btn_add', function(){
var nume_clasa= $('#nume_clasa').text();
if(nume_clasa == ''){
alert("Introdu numele clasei");
return false;
}
$.ajax({
url:"insert.php",
method:"POST",
data:{nume_clasa:nume_clasa},
dataType:"text",
success:function(data){
alert(data);
fetch_data();
}
});
});
function edit_data(id_clasa, text, nume_clasa){
$.ajax({
url:"edit.php",
method:"POST",
data:{id_clasa:id_clasa, text:text,nume_clasa:nume_clasa},
dataType:"text",
success:function(data){
alert(data);
}
});
}
$(document).on('blur','.nume_clasa', function(){
var id=$(this).data("id1");
var nume_clasa=$(this).text();
edit_data(id, nume_clasa, "nume_clasa");
});
$(document).on('click','.btn_delete', function(){
var id_clasa=$(this).data("id2");
if(confirm("Are you sure?")){
$.ajax({
url:"delete.php",
method:"POST",
data:{id_clasa:id_clasa},
dataType:"text",
success:function(data){
alert(data);
fetch_data();
}
});
}
});
});
</script>
If I use isset()
or !empty()
to check if they are declared before referencing them, the script seems to be executed but the data from the database is not deleted.