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

Asked

Viewed 11,175 times

2

Good night to you all, i would like to know how to open a form whose fields are already pre-filled with user data (resulting from a search in the database), being in charge of it only edit the information. I searched the internet and found a way, but would be generating the html of the whole form via command echo PHP; however I read that this is not a good programming practice... there is some way to do this in a way that respects good practices?

  • 1

    Use the attribute value to put the values in the form: <input value="João da Silva"> if it’s select place the attribute selected in the option e. g: <select><option value="H" selected>Homem</selected>.

2 answers

3


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

You would call the page by passing the values on 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 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
  • I liked this idea, but the code to set this variable, it would be in the same file along with html? or have as I do separately and only reference?

  • 1

    To assign the values can be in a php block on your page, where you will declare these variables and invoke the methods for the query. Now the query logic and everything else can be in a separate file, as good practice, to separate html from php.

  • 2

    If you want in I have an example I made a while ago.

  • if you can, I want to see yes, mt thanks

  • You’re welcome. I edited the answer with the code example.

1

The estrtura to pre-fill information from an HTML form is really 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:

  1. Make the quoting htmlspecialchars($variable) variables, so as not to run the risk of your HTML being pixoned by the contents of your variable.
  2. Avoid using table structure <table> to organize your form, prefer structure tableless, that make your code much more organized, separating the design and organization into a CSS file.

Browser other questions tagged

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