How to open a screen with pre-filled form with PHP and HTML?

2

Good evening everyone, I'd like to know how to open a form whose fields are already filled in with user data (resulting from a search in the database), and it's up to him to only edit the information. I searched the internet and found a way, but it would be generating the HTML of the whole form via PHP's echo command; however I read that this is not a good programming practice ... is there any way to do it in a way that respects good practices?

    
asked by anonymous 20.06.2015 / 00:52

2 answers

3

It would be interesting to have a value for a variable in the value attribute, so every time you load the data from the database you would assign the values. For example:

You would call the page passing the values in the request:

    <?php

    function __autoload($class) {

        if (file_exists("../app.ado/{$class}.class.php")) {
            include_once "../app.ado/{$class}.class.php";
        }
    }

    // Verifica se exitem valores passados na requisição - A mesma página serve para cadastro e edição,
    //  quando é aberta sem nenhum valor passado é para cadastro, caso contrário é edição
    if (empty($_REQUEST)) {
        // Variáveis que estão referenciadas nas textbox
        $id = "";
        $nome = "";
        $emailContato = "";
        $idInstituicaoEnsino = "";
        $tipo = "";
    } else {
        $id = $_REQUEST['id'];
        $nome = $_REQUEST['nome'];
        $emailContato = $_REQUEST['emailContato'];
        $idInstituicaoEnsino = $_REQUEST['idInstituicaoEnsino'];
        $tipo = $_REQUEST['tipo'];
    }

    ?>

And then we have the html:

    <form method="post" action="controller.php">
                    <table><!-- Campos de preenchimento-->
                        <!-- Identificação-->
                        <tr>
                            <td>
                                Identificação 
                            </td>
                            <td>
                                <input type="text" name="_id"
                                       value="<?= $id ?>"
                                       placeholder="Id Numérico" 
                                       size="10" title="Numero que identifica o curso"/>
                            </td>
                        </tr>
                        <!--Nome-->
                        <tr>
                            <td>
                                Nome
                            </td>
                            <td>
                                <input type="text" name="_nome"
                                       value="<?= $nome ?>"
                                       placeholder="Nome do curso" 
                                       size="50" title="Nome do curso"/>
                            </td>
                        </tr> ... E assim por diante
    
20.06.2015 / 01:18
0

The pretension to pre-fill information from an HTML form is actually very simple, even more using PHP.

<?php
  $nome_html = htmlspecialchars( $nome );
  $email_html = htmlspecialchars( $email );
?>

<form name="meuForm" method="post" id="formulario">
    <label for="nome">Nome</label>
    <input type="text"
           class="input_text"
           name="nome" id="name"
           value="<?= $nome_html ?>" />
    <label for="email">Email</label>
    <input type="text"
           class="input_text"
           name="email" id="email"
           value="<?= $email_html ?>" />
    <input type="button"
           class="button"
           value="Enviar" />
</form> 

Some tips:

  • Make the htmlspecialchars ($ variable) of variables, so as not to risk your HTML be pixed by the contents of your variable.
  • Avoid using <table> table structure to organize your form, prefer structure tableless , which make your code much more organized by separating design and organization into a CSS file.
  • 20.06.2015 / 20:15