Buonasera, io sto creando questa datatable collegata ad un DB, quando entro sul sito mi sta questo errore e io non capisco quale problema può esserci nel codice per la creazione. Nel recipe.php ho lasciato la parte in ajax. Grazie per l'aiuto
recipe.php:
recipe_action.php
recipe.php:
PHP:
<?php
<script>
$(document).ready(function(){
var dataTable = $('#table_data').DataTable({
"processing" : true,
"serverSide" : true,
"order" : [],
"ajax" : {
url:"../test/table_action.php",
type:"POST",
data:{action:'fetch'}
},
"columnDefs":[
{
"targets":[],
"orderable":false,
},
],
});
$('#add_table').click(function(){
$('#table_form')[0].reset();
$('#table_form').parsley().reset();
$('#modal_title').text('Add Data');
$('#action').val('Add');
$('#submit_button').val('Add');
$('#tableModal').modal('show');
$('#form_message').html('');
});
$('#table_form').parsley();
$('#table_form').on('submit', function(event){
event.preventDefault();
if($('#table_form').parsley().isValid())
{
$.ajax({
url:"../test/table_action.php",
method:"POST",
data:$(this).serialize(),
dataType:'json',
beforeSend:function()
{
$('#submit_button').attr('disabled', 'disabled');
$('#submit_button').val('wait...');
},
success:function(data)
{
$('#submit_button').attr('disabled', false);
if(data.error != '')
{
$('#form_message').html(data.error);
$('#submit_button').val('Add');
}
else
{
$('#tableModal').modal('hide');
$('#message').html(data.success);
dataTable.ajax.reload();
setTimeout(function(){
$('#message').html('');
}, 5000);
}
}
})
}
});
$(document).on('click', '.edit_button', function(){
var table_id = $(this).data('id');
$('#table_form').parsley().reset();
$('#form_message').html('');
$.ajax({
url:"../test/table_action.php",
method:"POST",
data:{id_ric:id_ric, action:'fetch_single'},
dataType:'JSON',
success:function(data)
{
$('#recipe_name').val(data.recipe_name);
$('#modal_title').text('Edit Data');
$('#action').val('Edit');
$('#submit_button').val('Edit');
$('#tableModal').modal('show');
$('#hidden_id').val(table_id);
}
})
});
$(document).on('click', '.delete_button', function(){
var id = $(this).data('id');
if(confirm("Are you sure you want to remove it?"))
{
$.ajax({
url:"../test/table_action.php",
method:"POST",
data:{id:id, action:'delete'},
success:function(data)
{
$('#message').html(data);
dataTable.ajax.reload();
setTimeout(function(){
$('#message').html('');
}, 5000);
}
})
}
});
});
</script>
recipe_action.php
PHP:
<?php
//table_action.php
include('../test/rms.php');
$object = new rms();
if(isset($_POST["action"]))
{
if($_POST["action"] == 'fetch')
{
$order_column = array('recipe_name');
$output = array();
$main_query = "
SELECT * FROM Ricette ";
$search_query = '';
if(isset($_POST["search"]["value"]))
{
$search_query .= 'WHERE recipe_name LIKE "%'.$_POST["search"]["value"].'%" ';
}
if(isset($_POST["order"]))
{
$order_query = 'ORDER BY '.$order_column[$_POST['order']['0']['column']].' '.$_POST['order']['0']['dir'].' ';
}
else
{
$order_query = 'ORDER BY id_ric DESC ';
}
$limit_query = '';
if($_POST["length"] != -1)
{
$limit_query .= 'LIMIT ' . $_POST['start'] . ', ' . $_POST['length'];
}
$object->query = $main_query . $search_query . $order_query;
$object->execute();
$filtered_rows = $object->row_count();
$object->query .= $limit_query;
$result = $object->get_result();
$object->query = $main_query;
$object->execute();
$total_rows = $object->row_count();
$data = array();
foreach($result as $row)
{
$sub_array = array();
$sub_array[] = html_entity_decode($row["recipe_name"]);
$sub_array[] = '
<div align="center">
<button type="button" name="edit_button" class="btn btn-warning btn-circle btn-sm edit_button" data-id="'.$row["table_id"].'"><i class="fas fa-edit"></i></button>
<button type="button" name="delete_button" class="btn btn-danger btn-circle btn-sm delete_button" data-id="'.$row["table_id"].'"><i class="fas fa-times"></i></button>
</div>
';
$data[] = $sub_array;
}
$output = array(
"draw" => intval($_POST["draw"]),
"recordsTotal" => $total_rows,
"recordsFiltered" => $filtered_rows,
"data" => $data
);
echo json_encode($output);
}
if($_POST["action"] == 'Add')
{
$error = '';
$success = '';
$data = array(
':recipe_name' => $_POST["recipe_name"]
);
$object->query = "
SELECT * FROM Ricette
WHERE recipe_name = :recipe_name
";
$object->execute($data);
if($object->row_count() > 0)
{
$error = '<div class="alert alert-danger">Table Already Exists</div>';
}
else
{
$data = array(
':recipe_name' => $object->clean_input($_POST["recipe_name"]),
);
$object->query = "
INSERT INTO Ricette
(recipe_name)
VALUES (:recipe_name)
";
$object->execute($data);
$success = '<div class="alert alert-success">Table Added</div>';
}
$output = array(
'error' => $error,
'success' => $success
);
echo json_encode($output);
}
if($_POST["action"] == 'fetch_single')
{
$object->query = "
SELECT * FROM Ricette
WHERE id_ric = '".$_POST["id_ric"]."'
";
$result = $object->get_result();
$data = array();
foreach($result as $row)
{
$data['recipe_name'] = $row['recipe_name'];
}
echo json_encode($data);
}
if($_POST["action"] == 'Edit')
{
$error = '';
$success = '';
$data = array(
':recipe_name' => $_POST["recipe_name"],
':id_ric' => $_POST['hidden_id']
);
$object->query = "
SELECT * FROM Ricette
WHERE recipe_name = :recipe_name
AND id_ric != :id_ric
";
$object->execute($data);
if($object->row_count() > 0)
{
$error = '<div class="alert alert-danger">Table Already Exists</div>';
}
else
{
$data = array(
':recipe_name' => $object->clean_input($_POST["recipe_name"])
);
$object->query = "
UPDATE Ricette
SET recipe_name = :recipe_name
WHERE id_ric = '".$_POST['hidden_id']."'
";
$object->execute($data);
$success = '<div class="alert alert-success">Table Updated</div>';
}
$output = array(
'error' => $error,
'success' => $success
);
echo json_encode($output);
}
if($_POST["action"] == 'delete')
{
$object->query = "
DELETE FROM Ricette
WHERE id_ric = '".$_POST["id"]."'
";
$object->execute();
echo '<div class="alert alert-success">Table Deleted</div>';
}
}
?>