PHP - How to send data from a table to a form [closed]

0
Hello, I have a problem getting the data from a static table and sending it to another page with the form to fill the input of this form with the data of the table. I would like help.

in the Customer.php list 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 database Customer.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>
    
asked by anonymous 06.02.2016 / 02:01

3 answers

3

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

 <a class="btn_padrao btn_margin" href="cadastroCliente.php?nome=<?= $nome ?>&email=<?= $email ?>">Editar</a>
    
06.02.2016 / 10:50
0

As I said before, I think it would be better to pass the data by GET in the url itself and do the data processing on the form page, now if you want to get it from a table and send it to a form on the same page you can identify the (td) with id or name and use javascript to take the data.

type

var tdnome = getElementById(id da td nome);
var campodoform = getElementById(id campo form);
campodoform.value = tdnome.innerHTML;
    
06.02.2016 / 17:12
0

The simplest way to resolve, without JavaScript, without any hassle, is to use hidden fields.

For this 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 passing values all over the internet when you have them on the server.

Therefore, the most secure and efficient method would be to just pass id as a URL parameter and load the other data on 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 such as Cross Site Scripting (XSS) and makes your code simpler.

    
08.02.2016 / 07:30