How could I POST the data by clicking on a button?
How could I POST the data by clicking on a button?
leelunaa
Posts: 7Questions: 0Answers: 0
in DataTables
Hi guys!
I have created a function, when clicking on a button, that should send the data from the line where this button is located to a php file.
Unfortunately, I couldn't do it. Can you give me any advice?
js code:
$('#tabela tbody').on('click', 'button', function () {
var dados = table.row($(this).parents('tr')).data();
var dados_json = JSON.stringify(dados);
// alert( "O ID é o " + dados[ 0 ]);
$.ajax({
url : 'action.php',
type: 'POST',
data: {data: dados_json},
success: function(result){
},
error: function(jqXHR, textStatus, errorThrown){
}
});
});
php: (A simple message to see if the relation is being well established)
<?php
$dados_recebidos = $_POST['data'];
$dados_convertidos = json_decode($dados_recebidos, true);
echo "<script type='text/javascript'>alert('Data received! ${dados_convertidos}');</script>";
Replies
Nothing immediately stands out as a problem. What happens? Do you get any errors?
Kevin
@kthorngren When I click on the button, nothing is actually happening
Maybe your selector (
$('#tabela tbody').on('click', 'button',
) is not correct? Does the event handler get called? Can you provide a link to your page or a test case replicating the issue so we can help debug?Kevin
@kthorngren I imagine it's being called correctly, because when I use the 'alert' function - which is commented in the code above - it works normally.
http://live.datatables.net/kagiseve/2/edit
Have you looked at the browser's network inspector to see if the Ajax (XHR) request is sent and what the response is?
Do you get errors in the console log?
Is the problem in the PHP script?
Kevin
Looks like everything is ok. Problably the problem is in the PHP script...
It was really my php script. Thanks for the help!