What is the number of the line clicked

Asked

Viewed 459 times

2

1]

I need to post this line number to another program php


<div id="posiciona">
    <table id="mostra_prod" cellpadding="1" cellspacing="3" bordercolor="#000">
        <tr>
            <td align="right" bgcolor="#0a0a96">Aplicação</td>
            <td align="left"  bgcolor="#0a0a96">Referência</td> 
        </tr> 
        <?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 
       }
       ?>         
    </table>
</div>
  • You could use something like onClick="minhaFuncao(this, <?= $i; ?>)". What you already tried?

  • 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".

  • Paulo, because in certain parts of the code you use the echo of PHP 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.

  • Paul is just by POST? What do you tell me about the GET?

  • Dear Marcos, I appreciate your return... I still don’t understand if I could be better for GET.

  • All right, but try to use it in one way only, the echo PHP prints the elements HTML, then you can always use the echo instead of closing and opening the PHP, or use the echo only where you will use the code in PHP same, opens <?php puts the code and then closes ?>... Ah and try to mark as RIGHT ANSWER the answer that really solved your problem ;)

Show 1 more comment

2 answers

0

What you can do simply is to enter a form field of the type radio in the table, along with a label, thus, when the table row is pressed, the field will be selected, containing the id of the record in question. For example, in the column Aplicação, put something like:

<td align='right' style='color: #cfcfd1;'>
  <label>
    Produto 1
    <input type="radio" name="aplicacao" value="1">
  </label>
</td>

When the line is pressed, the field radio will be selected containing the value 1. If you do not want the field to be visible in the table, just hide it:

<input type="radio" name="aplicacao" value="1" style="display:none;">

See a working example:

body {
  background: gray;
}
<table id="mostra_prod" cellpadding="1" cellspacing="3" bordercolor="#000">
  <tr>
    <td align="right" bgcolor="#0a0a96">Aplicação</td>
    <td align="left"  bgcolor="#0a0a96">Referência</td> 
  </tr>
  <tr>
    <td align='right' style='color: #cfcfd1;'>
      <label>
        Produto 1
        <input type="radio" name="aplicacao" value="1">
      </label>
    </td>
    <td style='color: #ffffff;'>0001</td>
  </tr>
  <tr>
    <td align='right' style='color: #cfcfd1;'>
      <label>
        Produto 2
        <input type="radio" name="aplicacao" value="2">
      </label>
    </td>
    <td style='color: #ffffff;'>0002</td>
  </tr>
  <tr>
    <td align='right' style='color: #cfcfd1;'>
      <label>
        Produto 3
        <input type="radio" name="aplicacao" value="3">
      </label>
    </td>
    <td style='color: #ffffff;'>0003</td>
  </tr>
</table>

Thus, with the code properly within a form, the field aplicacao will be sent with the selected line value.

  • Dear Anderson, I commend your collaboration, I will test your code...

0


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 !

  • Dear Lucas, good afternoon!

  • Dear Lucas, good afternoon! I thank you in advance, the fulfillment of my request. I, entered your code, and is not calling the second program. I also want to inform you that, as a first-time sailor, I thought I could pass the array parameter 'rfprod' by subscribing the line number. Blunder...! So, rephrasing, I need to submit the item reference field, $rfprod by POST.

  • @Pauloladeira Edited click Handler : )

Browser other questions tagged

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