Get column value from an HTML table and pass to PHP variable

1

I have already seen that there is a good example on the web of how to get the column value of a HTML table, but also how to pass the value of a variable JavaScript to PHP . But when I'm using the two together it's not working. I've done and refacted the code several times, it now looks like this:

index.php

<?php
session_start();
?>
<head>
    <script src="//code.jquery.com/jquery-2.1.1.min.js"></script>
    <meta charset="utf-8">
    <title>JS Bin</title>
</head>
<script>
    $(function () {
        $(document).on('click', '.btn-danger', function (e) {
            e.preventDefault;
            var codigo = $(this).closest('tr').find('td[data-nome]').data('nome');
            alert(codigo);
        });
    });

<?php
$variavelphp = "<script>document.write(codigo)</script>";
$_SESSION['codigo'] = "Este é Código:  $variavelphp";
?>
</script>
<body>
    <table>
        <thead>
            <tr>
                <th>Código</th>
                <th>Nome</th>
                <th>Ação</th>
            </tr>
        </thead>
        <tr>
            <td>111</td>
            <td data-nome="111">Joaquim Caetano</td>
            <td><a href="ValidaCliente.php"><button class="btn-danger">Pegar Valor do Código</button></a></td>
        </tr>
        <tr>
            <td>222</td>
            <td data-nome="222">Maria da Silva</td>
            <td><a href="ValidaCliente.php"><button class="btn-danger">Pegar Valor do Código</button></a></td>
        </tr>
    </table>
</body>
</html>

This is the page code that will use the session value created:

ValidaCliente.php

<?php
session_start();
$idCliente = $_SESSION['codigo'];
echo $idCliente;

On page index.php , the alert message is correct, but when arriving at ValidaCliente.php , only the fixed text that is added to the session and not the code is shown. Look at the pictures:

Thank you in advance for the help!

    
asked by anonymous 26.11.2016 / 15:41

1 answer

1

index.php :

<head>
    <script src="//code.jquery.com/jquery-2.1.1.min.js"></script>
    <meta charset="utf-8">
    <title>JS Bin</title>
</head>
<script>
    $(function () {
        $(document).on('click', '.btn-danger', function (e) {
            e.preventDefault;
            var codigo = $(this).closest('tr').find('td[data-nome]').data('nome');
            $.get( "CriaSession.php",{ codigo: codigo});
        });
    });
</script>
<body>
    <table>
        <thead>
            <tr>
                <th>Código</th>
                <th>Nome</th>
                <th>Ação</th>
            </tr>
        </thead>
        <tr>
            <td>111</td>
            <td data-nome="111">Joaquim Caetano</td>
            <td><a href="ValidaCliente.php"><button class="btn-danger">Pegar Valor do Código</button></a></td>
        </tr>
        <tr>
            <td>222</td>
            <td data-nome="222">Maria da Silva</td>
            <td><a href="ValidaCliente.php"><button class="btn-danger">Pegar Valor do Código</button></a></td>
        </tr>
    </table>
</body>
</html>

CriaSession.php

<?php
if(!isset($_SESSION)){session_start();}

if (isset($_GET['codigo'])){
    $_SESSION['codigo'] = $_GET['codigo'];
}
else {
    $_SESSION['codigo'] = NULL;
}

ValidaCliente.php

<?php
if(!isset($_SESSION)){session_start();}
$idCliente = $_SESSION['codigo'];
echo $idCliente;
    
26.11.2016 / 22:32