Well, we have some things to do here:
1º we will give a fix on your marking, placing the elements due for each part of the table:
<div id="posiciona">
<table id="mostra_prod" cellpadding="1" cellspacing="3" bordercolor="#000">
<thead>
<tr>
<th align="right" bgcolor="#0a0a96">Aplicação</th>
<th align="left" bgcolor="#0a0a96">Referência</th>
</tr>
</thead>
<tbody>
<?php
$i=0;
while ($row = mysqli_fetch_array($result_pd)) {
$rfprod = $row['pro_referencia_produto'];
$idref[] = $rfprod;
$i++;
$approd = $row['pro_aplicacao_produto'];
?><tr><?php
echo "<td align='right' style='color: #cfcfd1;'>".$approd."</td>";
echo "<td style='color: #ffffff;'>".$rfprod."</td>";
?></tr><?php
}
?>
</tbody>
</table>
</div>
We put the elements thead
and tbody
which are the table header and body respectively.
2º Add the javascript:
function enviar(caminho, parametros, metodo) {
metodo = metodo|| 'post'; // O padrão será post.
var form = document.createElement( 'form' );
form.setAttribute( 'method', metodo );
form.setAttribute( 'action', caminho );
//Cria os campos com base em um objeto passado
for( var propriedade in parametros )
if( parametros.hasOwnProperty( key ) ) {
var campo = document.createElement( 'input' );
campo.setAttribute( 'type', 'hidden' );
campo.setAttribute( 'name', key );
campo.setAttribute( 'value', parametros[propriedade] );
form.appendChild( hiddenField );
}
document.body.appendChild( form );
//Envia o formulário
form.submit();
}
//pegar uma array com as linhas:
const linhas = Array.prototype.slice.call( document.querySelectorAll( '#mostra_prod tbody tr' ) );
const onClick = function() {
//Quando clicar em uma linha (tr) vai enviar o conteudo da sua segunda celula (td):
enviar( 'meu_arquivo.php', { linha : this.children[1].innerText } );
}
//Adicionamos o listener
linhas.forEach( linha => linha.addEventListener( 'click', onClick ) );
3º now just take the variable in meu_file.php :
$linha = $_POST['linha'];
Note: I sincerely recommend the jQuery library and recommend the use of asynchronous requests, not that it won’t work, but this makes the application richer, providing a better user experience !
You could use something like
onClick="minhaFuncao(this, <?= $i; ?>)"
. What you already tried?– Sergio
You can work with Cookie or with Session, so you store the information you want in variables and can retrieve it in any other php "program".
– Marcos Henzel
Paulo, because in certain parts of the code you use the
echo
ofPHP
to print the<td>
and in other parts you use?> <td> <?php
, so it gets confusing even for you to give some future maintenance, I advise to do just in a way.– Marcos Henzel
Paul is just by POST? What do you tell me about the GET?
– Bsalvo
Dear Marcos, I appreciate your return... I still don’t understand if I could be better for GET.
– Paulo Ladeira
All right, but try to use it in one way only, the
echo
PHP prints the elementsHTML
, then you can always use theecho
instead of closing and opening thePHP
, or use theecho
only where you will use the code inPHP
same, opens<?php
puts the code and then closes?>
... Ah and try to mark as RIGHT ANSWER the answer that really solved your problem ;)– Marcos Henzel