pass hidden variables and retrieve it in another page

0

I have a task calendar and a "Perform" tag

this is my code

<section id="form">
    <form class="contact_form" ACTION="prueba.php" METHOD="POST">
        <ul>
            <li>
                <h2>Calendario de Tareas</h2>
            </li>
            <?php
$sql="select Nombre from login where user_name ='$usuario' ";
$result= mysqli_query($conexion,$sql) or die(mysqli_error());
$user = $result->fetch_assoc()['Nombre'];
            ?>
            <?php
/* Realizamos la consulta SQL */
$sql="select * from tareas where Auditor ='$user'";
$result= mysqli_query($conexion,$sql) or die(mysqli_error());
if(mysqli_num_rows($result)==0) die("No hay registros para mostrar");
  
echo "<table border=0 cellpadding=15 cellspacing=3>";

/*Priemro los encabezados*/
 echo "<tr>
         <th colspan=16 BGCOLOR='#BCC1C2' WIDTH='10%' face='Georgia' align='center'> Auditorias Programadas </th>
       <tr BGCOLOR='#DADDDE'>
         <th class='estilo1'> ID </th><th class='estilo1'> Auditor </th><th class='estilo1'> Auditoria </th>
         <th class='estilo1'> Fecha Deseada </th><th class='estilo1'> Fecha limite </th><th class='estilo1'> Estado </th><th class='estilo1'> Realizar </th>
      </tr>";

/*Y ahora todos los registros */
while($row=mysqli_fetch_array($result))
{
    $id = $row["ID"];
    $Auditor = $row["Auditor"];
    $auditoria = $row["Auditoria"];
    $Fdeseada = $row["Fdeseada"];
    $Flimite = $row["Flimite"];
 echo "<tr>
         <td align='right' class='estilo1'> $id </td>
         <td class='estilo1'> $Auditor </td>
         <td class='estilo1'> $auditoria </td>
         <input type='hidden' name='auditoria' value='".$auditoria."' />
        <td class='estilo1'> $Fdeseada </td>
         <td class='estilo1'> $Flimite </td>
             <td class='estilo1'> $row[Estado] </td>
                 <td class='estilo1'><A HREF='prueba.php'> Realizar </A></td>
              <input type='hidden' name='oculto' value='".$id."' />
                     
                 
      </tr>";
}
echo "</table>";

?>
    </form>        
</section>

and I want to pass the id and audit selected to another page in php

page 2

<?php
$Operarios= $_GET['auditoria'];
$id = ['oculto'];
echo $Operarios;
echo $id;
?>
    
asked by antonio sanchez 19.04.2018 в 17:50
source

3 answers

4

You can try adding your variable $ id to the url and then you get it via $ _GET ['id'] in another sight

In your code you could update the tag to:

<a href='prueba.php?id=".$id."'> Realizar </a>

And on page 2:

$id = $_GET['id'];//variable con el id pasado

I would recommend using a POST method better to avoid passing the variables through the URL, but functionally it works.

I hope I have served you!

    
answered by 19.04.2018 / 18:08
source
0

In order to send by POST the ideal is that you send the form as such. And since you need to send the values of only 1 of the records in the table / grid, you can capture those values using Javascript and assign them to the hidden controls, to then force the submission of the form.

You already have the idea of hidden controls, but in your case, they should not be for each row, but only once. If you sent the form as it is, the values of the hidden fields would undoubtedly reach the other side, but with the value of only the last line. (If you really wanted to send them all, you would have to name those hidden controls in array mode ( name="oculto[]" ), so that the other side will arrive as array , but this is not your case now.)

In your form, the hidden fields have to go only once:

<section id="form">
    <form class="contact_form" ACTION="prueba.php" METHOD="POST" id="formulario">
        <input type="hidden" name="auditoria" id="auditoria" />
        <input type="hidden" name="oculto" id="oculto" />
        <ul>
            <li>
                <h2>Calendario de Tareas</h2>
            </li>

Notice that I gave the form an "id". It will be necessary later.

Then, in each row, you apply a handler for the event onclick of each row. You can do it on the label <tr> , <td> or on a button, or even on the link you already have ... You choose where. I'll show you the example about the <tr> tag:

<?php
/*Y ahora todos los registros */
while($row=mysqli_fetch_array($result))
{
    $id = $row["ID"];
    $Auditor = $row["Auditor"];
    $auditoria = $row["Auditoria"];
    $Fdeseada = $row["Fdeseada"];
    $Flimite = $row["Flimite"];
 echo "<tr onclick=\"enviarRegistro('".$auditoria."', '".$id."')\">
         <td align='right' class='estilo1'> $id </td>
         <td class='estilo1'> $Auditor </td>
         <td class='estilo1'> $auditoria </td>
        <td class='estilo1'> $Fdeseada </td>
         <td class='estilo1'> $Flimite </td>
             <td class='estilo1'> $row[Estado] </td>
                 <td class='estilo1'><A HREF='prueba.php'> Realizar </A></td>
      </tr>";
} 
?>

Where the Javascript function enviarRegistro() would be something like:

function enviarRegistro(auditoria, id)
{
  document.getElementById("auditoria").value = auditoria;
  document.getElementById("id").value = id;
  document.getElementById("formulario").submit();
}

Of course you can change the use of basic Javascript by JQuery, or whatever you want. My intention is to give you an idea of the logic, you will see how you implement it;)

    
answered by 19.04.2018 в 19:04
0

As such, the type does not matter: hidden , text , number .

It happens that they are inputs with a " name " therefore you must do a POST of them. Unless you pass them by url with a HREF , which for this case if you use GET

There would be something like this in the action of your file called prueba.php

$id = $_POST["id"]
$auditoria = $_POST["auditoria"]

Now, you want to pass that data from a table, I guess you want a button that appears in each column, this should be like this:

<a href="prueba.php?id='.$id.'"></a>

In the prueba.php file we will use the following

$id = $_GET["id"]
    
answered by 19.04.2018 в 19:14