How could I POST the data by clicking on a button?

How could I POST the data by clicking on a button?

leelunaaleelunaa Posts: 7Questions: 0Answers: 0

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

  • kthorngrenkthorngren Posts: 21,303Questions: 26Answers: 4,947

    Nothing immediately stands out as a problem. What happens? Do you get any errors?

    Kevin

  • leelunaaleelunaa Posts: 7Questions: 0Answers: 0

    @kthorngren When I click on the button, nothing is actually happening

  • kthorngrenkthorngren Posts: 21,303Questions: 26Answers: 4,947

    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

  • leelunaaleelunaa Posts: 7Questions: 0Answers: 0
    edited September 2021

    @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

  • kthorngrenkthorngren Posts: 21,303Questions: 26Answers: 4,947
    edited September 2021

    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

  • leelunaaleelunaa Posts: 7Questions: 0Answers: 0


    Looks like everything is ok. Problably the problem is in the PHP script...

  • leelunaaleelunaa Posts: 7Questions: 0Answers: 0

    It was really my php script. Thanks for the help! :wink:

Sign In or Register to comment.