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.php
the 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:
Thanks in advance for your help!
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.
– JuniorNunes
Completing what @Juniornunes said above. Keep in mind that PHP runs on the server while Javascript runs on the client(browser)
– Giovane
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.
– Joaquim Caetano Teixeira