send by POST when the id is variable

1

On a page I created a table that when clicking on a row it sends to another page. On the other page I want to collect the information by POST and not by GET.

The problem is that each row has a different ID and not consecutive, so when trying to collect the value on the next page, I do not have a unique ID.

My table looks like this:

<form name="formListado" action="../rep/detalleDir.php" method="POST">
  <table name="tableRoster" id="id_tableRoster">
    <thead id="encabezados">...</thead>

    <tbody>
      <?php foreach ($res_listado as $key => $value) { ?>
        <tr name="idmedico" id="<?php echo $value['id_unico']; ?>" onclick="enviar2(this.id)">
          <td align="center"> ... </td>            
        </tr>
        <?php } ?>
     </tbody>
   </table>

I was thinking of using a script to send to the next page but I do not finish putting it together.

function enviar2 (idorigen) {
            var idMed = idorigen;
            document.getElementsByName('idmedico').value = idMed;
            document.formListado.submit();
}

And finally on the other page, it does not do me any good to have a $_POST["algo"] because I never know what number is going to arrive.

Any suggestions on how I can send this data ?, As I said before I can not use GET because they are sensitive data that I do not want to have in the navigation bar.

    
asked by Francisco 30.03.2018 в 20:22
source

2 answers

0

1. If the only thing you are going to send is inside the row, then make a form for each row (and hide it with styles).

<?php
$res_listado = array(
array(
    'id_unico'=>1,
    'nombre'=>'Juan Perex',
    'datovergonzoso'=>'le huelen los pies',
),
array(
    'id_unico'=>2,
    'nombre'=>'Arnaldo Biroya',
    'datovergonzoso'=>'ronca en Do mayor',
),
array(
    'id_unico'=>3,
    'nombre'=>'Emilio Fuentes',
    'datovergonzoso'=>'No sabe comer chicles',
),
array(
    'id_unico'=>4,
    'nombre'=>'Rodolfo Innuit',
    'datovergonzoso'=>'le da verguenza decirlo',
),
);
?>
<!DOCTYPE html>
<html lang="es">
<head>
<title>datatablesvaloroculto</title>
<style>
.markrow{background:#fe0;}
</style>
</head>
<body>
<table name="tableRoster" id="id_tableRoster">
<thead id="encabezados">...</thead>
<tbody>
  <?php foreach ($res_listado as $key => $value) { ?>
    <tr onclick="enviar2(<?php echo $value['id_unico']; ?>)">
      <td align="center">
      <form name="formListado-fila-<?php echo $value['id_unico'];?>" action="detalleDir.php" method="POST" style="display:none;">
      <input name="idMedico" type="hidden" value="<?php echo $value['id_unico']; ?>" />
      </form>
       <?php echo $value['nombre'];?>
      </td>
      <td>
       <?php echo $value['datovergonzoso'];?>
      </td>

    </tr>
    <?php } ?>
 </tbody>
</table>
</body>
<script>
  function enviar2(id) {
    elForm= document.getElementsByName('formListado-fila-'+id)[0];
    elForm.submit();
  }
</script>
</html>

the detailDir.php receives what you send in the form corresponding to the row clicked (in this case there is only one input called "idMedico"):

<?php
var_dump($_POST);
?>

will result in:

array(1) {
  ["idMedico"]=>
 string(1) "2"

}

if you click on row 2

If you need to send more own data of each row (row) you include them as inputs within the form.

2. If instead the extra data is common to all the rows and would be repeated a lot, then what you can do is create the form outside the table, add a hidden field for the id and set it before submit:

<?php
$res_listado = array(
array(
    'id_unico'=>1,
    'nombre'=>'Juan Perex',
    'datovergonzoso'=>'le huelen los pies',
),
array(
    'id_unico'=>2,
    'nombre'=>'Arnaldo Biroya',
    'datovergonzoso'=>'ronca en Do mayor',
),
array(
    'id_unico'=>3,
    'nombre'=>'Emilio Fuentes',
    'datovergonzoso'=>'No sabe comer chicles',
),
array(
    'id_unico'=>4,
    'nombre'=>'Rodolfo Innuit',
    'datovergonzoso'=>'le da verguenza decirlo',
),
);
?>
<!DOCTYPE html>
<html lang="es">
<head>
<title>datatablesvaloroculto</title>
<style>
.markrow{background:#fe0;}
</style>
</head>
<body>
<form name="formListado" action="detalleDir.php" method="POST">
<input name="idMedico" type="hidden" value="desconocido" />
<input name="unCampoGenerico" type="hidden" value="informacion oculta no tan oculta" />
<input name="otroCampoGenerico" type="hidden" value="otra informacion oculta no tan oculta" />
deja un comentario antes de clickar:
<input name="comentario" type="text" />
<table name="tableRoster" id="id_tableRoster">
<thead id="encabezados">...</thead>
<tbody>
  <?php foreach ($res_listado as $key => $value) { ?>
    <tr onclick="enviar2(<?php echo $value['id_unico']; ?>)">
      <td align="center">
       <?php echo $value['nombre'];?>
      </td>
      <td>
       <?php echo $value['datovergonzoso'];?>
      </td>

    </tr>
    <?php } ?>
 </tbody>
</table>
      </form>
</body>
<script>
  function enviar2(id) {
    elInput= document.getElementsByName('idMedico')[0];
    elInput.value=id;
    elForm= document.getElementsByName('formListado')[0];
    elForm.submit();
  }
</script>
</html>

example of the received with the second code:

array(4) {
  ["idMedico"]=>
  string(1) "3"
  ["unCampoGenerico"]=>
  string(32) "informacion oculta no tan oculta"
  ["otroCampoGenerico"]=>
  string(37) "otra informacion oculta no tan oculta"
  ["comentario"]=>
  string(18) "voy a clickar el 3"
}
    
answered by 31.03.2018 в 04:14
0

Try removing the "name to" and add a hidden input below the form. and keep using your same script to do the submit.

What we are correcting is that he does not have the value attribute that does the POST method, they are only accepted for input, select and textarea.

I hope I help you!

<form name="formListado" action="../rep/detalleDir.php" method="POST">

  <input type="hidden" name="idmedico"/>
  
  <table name="tableRoster" id="id_tableRoster">
    <thead id="encabezados">...</thead>

    <tbody>
      <?php foreach ($res_listado as $key => $value) { ?>
        <tr id="<?php echo $value['id_unico']; ?>" onclick="enviar2(this.id)">
          <td align="center"> ... </td>            
        </tr>
        <?php } ?>
     </tbody>
   </table>
</form>
    
answered by 02.04.2018 в 22:52