What I want to do:
- I'm trying to send data to my server using
$.postjQuery shortcut.
What is the problem:
- My program doesn't enter in my
switch case, maybe because myactionit isn't sending properly.
What I'm trying to do:
I created a table and one of my elements is a button with an
onclickJavaScript function:<button onclick='Delete()'></button>.My JavaScript function:
function Delete(){
$.post("<?=SITE_URL_ADMIN?>/alexcrudgenerator/res/?action=deleteRegistro", function(data){
if(data){
console.log("Deleteeeed");
}
else {
console.log("Not deleted :(");
}
});
}
- My
switch casestatement:
switch($_POST['action']){
case 'deleteRegistro': // NOT OK
?>
<script>
alert("I'm in deleteRegistro");
</script>
<?php
break;
}
- What happen when I click on the button:
This, should work because I'm passing my action deleteRegistro in my post request to my server (php).
I need some help, what I'm doing wrong?
This is my full code if you want to check it out:
<?php
include_once(DIR_PLUGINS.'/alexcrudgenerator/main.php');
$test = new GenerateCrud($_POST['tableName'], $_POST['id'], $_POST['tableFields']);
switch($_POST['action']){
case 'datosTabla': // OK.
$res = json_decode($_POST['datos']);
echo json_encode($res, JSON_UNESCAPED_UNICODE);
break;
case 'deleteRegistro': // NOT OK
?>
<script>
alert("I'm in deleteRegistro");
</script>
<?php
break;
case 'showtable': // OK.
$res = getEntireTable($_POST['tableName'], $_POST['id'], $_POST['tableFields']);
$tableName = $_POST['tableName'];
foreach ($res as $data){
$data->acción = "<div class='text-center'><div class='btn-group'><button id='modificar_$data->id' class='btn btn-primary btn-sm btnEditar' value='edit'><i class='material-icons'>edit</i></button><button onclick='Delete()' class='btn btn-danger btn-sm btnBorrar'><i class='material-icons' value='delete'>delete</i></button></div></div>";
$resultados['data'][] = $data;
}
$resultados = json_encode($resultados);
foreach(json_decode($_POST['tableFields']) as $columnsDB){
$fields[] = array('data'=>$columnsDB);
}
$fields[]['data'] = 'acción';
$fields = json_encode($fields);
?>
<head>
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
</head>
<div class="container caja">
<div class="row">
<div class="col-lg-12 col-sm-12">
<div>
<table id="tablaUsuarios" class="table table-striped table-bordered table-condensed hover" style="width:100%" >
<thead class="text-center">
<tr>
<?php
foreach (json_decode($_POST['tableFields']) as $columnsTH){
echo '<th>' . strtoupper($columnsTH) . '</th>';
}
echo '<th>ACCIÓN</th>';
?>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
</div>
</div>
<script>
function Delete(){
$.post("<?=SITE_URL_ADMIN?>/alexcrudgenerator/res/?action=deleteRegistro", function(data){
if(data){
console.log("Deleteeeed");
}
else {
console.log("Not deleted :(");
}
});
}
$(document).ready(function() {
var datos= <?=$resultados?>;
var dynamicColumns = <?=$fields?>;
datos = JSON.stringify(datos);
$('#tablaUsuarios').DataTable({
"language": {"url": "https://cdn.datatables.net/plug-ins/1.10.25/i18n/Spanish.json"},
"paging": true,
"lengthChange": true,
"searching": true,
"info": true,
"autoWidth": true,
"scrollX": true,
"ajax":{
"url": '<?=SITE_URL_ADMIN?>/alexcrudgenerator/crud/res/',
"method": 'POST',
"data":{action: "datosTabla", datos: datos}
},
"columns": dynamicColumns
});
})
</script>
<?php
break;
}
?>

actionas part of the request body instead of sending it as part of the URL. jQuery'spostmethod api.jquery.com/jquery.post accepts adataproperty, which you can to pass your action to the server as part of the request body. Something likedata: { action: 'deleteRegistro' }