PHP - How to send data from a table to a form

Asked

Viewed 1,843 times

0

Hello, I’m having a problem picking up the dice one-table static and send to another page with the form to fill in the input that form with the dice table. I would like help.

in the list Client.php has:

 <table class="table">
    <tr class="tb_column tb_cabecalho">
        <td>ID</td>
        <td>Nome</td>
        <td>E-mail</td>
        <td>Ações</td>
    </tr>
    <tbody>
    <form action="" method="get">
        <?php
        $id = "1";
        $nome = "José Carlos";
        $email = "[email protected]";
        ?>
        <tr class="cinza">
            <td><?php echo $id; ?></td>
            <td><?php echo$nome; ?></td>
            <td><?php echo$email; ?></td>
            <td>
                <button class="btn_padrao btn_margin" type="submit" title="Editar" alt="[Editar]"><a href="index.php?link=4">Editar</a></button>
                <button class="btn_padrao btn_margin" title="Excluir" alt="[Excluir]">Excluir</button>
                <button class="btn_padrao btn_margin" title="Matricular" alt="[Matricular]">Matricular</button>
            </td>
        </tr>
    </form>
    </tbody>
</table>

in the register.php has:

<form>
        <?php
        @$nome = $_GET['nome'];
        @$email = $_GET['email'];
        echo $nome;
        ?>
        <label>Nome</label>
        <input type="text" name="nome" value="<?php echo $nome; ?>" />

        <label>E-mail</label>
        <input type="email" name="email" value="<?php echo $email; ?>"/>

        <div class="me">
            <label>Senha</label>
            <input type="text" name="senha" />
        </div>
    </form>

3 answers

3

Instead of creating a form inside the table and use buttons for actions, why not use links and pass values through the URL? Example:

 <a class="btn_padrao btn_margin" href="cadastroCliente.php?nome=<?= $nome ?>&email=<?= $email ?>">Editar</a>
  • Thank you very much. It helped me a lot. It worked

0

The simplest way to solve, without Javascript, without gambiarra, is by using hidden fields.

For that, you will need a form for each line.

Example:

<table class="table">
    <tr class="tb_column tb_cabecalho">
        <td>ID</td>
        <td>Nome</td>
        <td>E-mail</td>
        <td>Ações</td>
    </tr>
    <tbody>
        <?php
        $id = "1";
        $nome = "José Carlos";
        $email = "[email protected]";
        ?>
        <tr class="cinza">
            <td><?php echo $id; ?></td>
            <td><?php echo $nome; ?></td>
            <td><?php echo $email; ?></td>
            <td>
              <form action="index.php" method="get">
                <input type="hidden" name="id" value="<?php echo $id ?>"/>
                <input type="hidden" name="nome" value="<?php echo $nome ?>"/>
                <input type="hidden" name="email" value="<?php echo $email ?>"/>
                <button class="btn_padrao btn_margin" type="submit" title="Editar" alt="[Editar]" name="action" value="editar">Editar</button>
                <button class="btn_padrao btn_margin" title="Excluir" alt="[Excluir]" name="action" value="excluir">Excluir</button>
                <button class="btn_padrao btn_margin" title="Matricular" alt="[Matricular]" name="action" value="matricular">Matricular</button>
              </form>
            </td>
        </tr>
    </tbody>
</table>

However, I generally do not recommend going through values all over the internet when you have them on the server.

Therefore, the safest and most efficient method would be to just pass the id as a URL parameter and load the other data into the registration page.

Example:

<table class="table">
    <tr class="tb_column tb_cabecalho">
        <td>ID</td>
        <td>Nome</td>
        <td>E-mail</td>
        <td>Ações</td>
    </tr>
    <tbody>
        <?php
        $id = "1";
        $nome = "José Carlos";
        $email = "[email protected]";
        ?>
        <tr class="cinza">
            <td><?php echo $id; ?></td>
            <td><?php echo $nome; ?></td>
            <td><?php echo $email; ?></td>
            <td>
                <a href="index.php?id=<?php echo $id ?>" class="btn_padrao btn_margin">Editar</a>
                <a href="excluir.php?id=<?php echo $id ?>" class="btn_padrao btn_margin">Excluir</s>
                <a href="matricula.php?id=<?php echo $id ?>" class="btn_padrao btn_margin">Matricular</a>
            </td>
        </tr>
    </tbody>
</table>

This approach greatly reduces the risk of attacks like XSS (Cross Site Scripting) and makes your code simpler.

0

As it has already been passed I believe it would be better to already pass the data by GET in the own url and do the processing of the data in the form page, now if you want to take a table and send to a form in the same page you can identify the (td) with id or name and use javascript to carry the data.

guy

var tdnome = getElementById(id da td nome);
var campodoform = getElementById(id campo form);
campodoform.value = tdnome.innerHTML;
  • Thank you so much for your help

Browser other questions tagged

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