Passing data from a table to a php form

0

Good, I want to pass the data that appears in my table to a form on another page, how do I do it with only php? The code I have is:

<?php 
include('conexion.php');
include('busqueda.php');

$busqueda = $_GET['buscar'];
$query = "select * from datos where nombre like '%$busqueda%'";
$consulta = pg_query($conexion, $query);

?>
<!DOCTYPE html>
<html>
<head>
    <title> Resultados </title>
    <style>
        table tr td{
            text-align: center;

        }
        table{
            width: 80%;
            margin-top: 10%;
        }
    </style>
</head>
<body>
<center>
    <form method="POST">
        <table border="3">
            <tr>
                <td> ID </td>
                <td> Nombre </td>
                <td> Cedula </td>
                <td> Curso </td>
                <td> Fecha de inicio </td>
                <td> Fecha de culminación </td>
            </tr>
        <?php while($fila = pg_fetch_assoc($consulta)){ ?>
            <tr>
                <td> <?php echo $fila['id']; ?> </td>
                <td> <?php echo $fila['nombre']; ?> </td>
                <td> <?php echo $fila['cedula']; ?> </td>
                <td> <?php echo $fila['curso']; ?> </td>
                <td> <?php echo $fila['inicio']; ?> </td>
                <td> <?php echo $fila['fin']; ?> </td>
                <td> <input type="submit" name="boton" value="Ver"> </td>
            </tr>
        <?php } ?>
        </table>
    </form>
</center>
</body>
</html>

When I input the "View", so that I just sent the data from that section of the table to the form on another page.

    
asked by Edwin Aquino 25.04.2017 в 16:56
source

2 answers

1

You can add these variables to inputs with the property hidden , this way they will be hidden and when you click on the "see" button you can send them by POST to another page, example:

<form method="POST" action="paginadestino.php">
    <table border="3">
        <tr>
            <td> ID </td>
            <td> Nombre </td>
            <td> Cedula </td>
            <td> Curso </td>
            <td> Fecha de inicio </td>
            <td> Fecha de culminación </td>
        </tr>
    <?php while($fila = pg_fetch_assoc($consulta)){ ?>
        <tr>
            <td> <?php echo $fila['id']; ?> </td>
            <td> <?php echo $fila['nombre']; ?> </td>
            <!--Resto de campos-->

            <input type="hidden" name="id" value="<?php echo $fila['id']; ?>">
            <input type="hidden" name="nombre" value="<?php echo $fila['nombre']; ?>">
            <!--Resto de campos-->

            <td> <input type="submit" name="boton" value="Ver"> </td>
        </tr>
    <?php } ?>
    </table>
</form>

Do not forget to put the destination page in property action of the form.

On the destination page you would receive the fields as follows:

<?php 
    $id = $_POST['id']; 
    $nombre = $_POST['nombre'];
    //Resto de campos...
?>

And you could add them to inputs of a new form in the following way:

<form method="POST" action="">
   <input type="text" name="nombre" value="<?php echo $nombre; ?>">
   <!-- Resto de campos !-->
</form>

You could also store these variables in session, in this way they would persist between page and page:

<?php
 while($fila = pg_fetch_assoc($consulta)){ 
     $_SESSION['nombre'] = $fila['nombre'];
     // Resto de campos ...
 }
?>
    
answered by 25.04.2017 в 17:10
1

Adding to the previous comment you can do it with a foreach fill in the data of the table and place a counter, that is, for each row you add a number and that number is associated with the input as follows

<form method="POST" action="paginadestino.php">
    <table border="3">
        <tr>
            <td> ID </td>
            <td> Nombre </td>
            <td> Cedula </td>
            <td> Curso </td>
            <td> Fecha de inicio </td>
            <td> Fecha de culminación </td>
        </tr>
    <?php $contador = 1;
          foreach($consulta as $fila){ ?>
        <tr>
            <td> <?php echo $fila['id']; ?> </td>
            <td> <?php echo $fila['nombre']; ?> </td>
            <!--Resto de campos-->

            <input type="hidden" name="id_.''.<?php echo $contador; ?>" value="<?php echo $fila['id']; ?>">
            <input type="hidden" name="nombre_.''.<?php echo $contador; ?>" value="<?php echo $fila['nombre']; ?>">
            <!--Resto de campos-->

            <td> <input type="submit" name="boton" value="Ver"> </td>
        </tr>
    <?php $contador = $contador + 1; } ?>
    </table>
</form>

What does the code do, is to associate a different name to the input so if you have more than one row when you send it the fields will be different because the problem that happens to you when you have more than one row and having the same name data is stepped

I hope I have helped

    
answered by 15.03.2018 в 15:32