Send a jquery variable via ajax to PHP

Asked

Viewed 50 times

1

I’m trying to send data from a table via Ajax to PHP. I’m doing this by transforming the table into JSON and sending it through Ajax to PHP. The scritp is like this:

<script type="text/javascript">
    $('#converter-tabela').click( function(){
        //alert("chegou!");    
        var table = $('#tabela').tableToJSON();
        console.log(table);
        alert(JSON.stringify(table)); 
        // Você envia os dados para o PHP utilizando AJAX
        $.ajax({
            // Enviamos os dados da tabela para um script PHP
            url: 'teste.php'
            , data: { 'dados_tabela': table }
            , method: 'POST'
        }).then(function(result) {
        // No retorno, apenas confirmamos
            if (result.success) {
                alert('Enviado para o servidor com sucesso!');
            }
        }).fail(function(result) {
            console.log(result);
        });
    });

and in PHP I’m only asking to show on screen if everything went well:

<?php

ini_set('display_errors',1);
ini_set('display_startup_erros',1);
error_reporting(E_ALL);

var_dump($_POST['dados_tabela']);
 print_r($_POST['dados_tabela']);?>

Only in the script only comes up to the alert(JSON.stringify(table)); . Ajax’s requisition doesn’t seem to be working. I’m using these links to the : Ajax

and JSON

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.