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

Asked

Viewed 2,954 times

2

I have seen that there is quite an example on the web of how to get the column value of a table HTML, as well as passing the value of a variable JavaScript to the PHP. However, when I’m using the two together it’s not working. I’ve done and re-did several times the code, it’s now this way:

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 code of the page that I will use the value of the created session:

Validaclient.php

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

On the page index.phpthe warning message is correct, but when arriving at ValidaCliente.php only fixed text is shown which is added to the session and not the code. See images: inserir a descrição da imagem aqui

Thanks in advance for your help!

  • 1

    You’ll have to use ajax mano, this way will not work, because PHP loads before, only after PHP finishes the execution, which the page loads with Javascript.

  • 1

    Completing what @Juniornunes said above. Keep in mind that PHP runs on the server while Javascript runs on the client(browser)

  • Well remembered, as I am still learning the language some of these details I sometimes end up forgetting, but I will see how I can do these procedures here for my example.

1 answer

2


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;
}

Validaclient.php

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

Browser other questions tagged

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