If you click on the displayed ID, it opens another page with a pre-filled form with the information

Asked

Viewed 47 times

0

Good afternoon to you all! I am a beginner in WEB programming and I have a big doubt, I created a previous form where the person type the data and is saved in the database, I also created another screen where displays all the results in a table, now on this screen where is inside a table, wanted to put that when clicked on the ID, it opens the form again with the information of the clicked ID already filled. Please, if you could help me I would be very grateful. The codes are:

NOTE: I’m not asking you to assemble for me, I just want some hint of how I can do this, I’m researching but I’m not getting it very well, thank you!

FORM:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Ficha de cadastro</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script src="http://cdnjs.cloudflare.com/ajax/libs/jquery.maskedinput/1.4.1/jquery.maskedinput.min.js"></script>
    <script type="text/javascript">

        function validar_form(){
            var nome = formcontato.nome.value;
            var snome = formcontato.snome.value;
            var datanasc = formcontato.datanasc.value;
            var endereco = formcontato.endereco.value;
            var numero = formcontato.numero.value;
            var bairro = formcontato.bairro.value;
            var telefone = formcontato.telefone.value;


            if (nome == "") {
                alert("campo nome é obrigatório");
                formcontato.nome.focus();
                return false;
            }if (snome == "") {
                alert("campo sobrenome é obrigatório");
                formcontato.snome.focus();
                return false;
            }if (datanasc == "") {
                alert("campo data de nascimento é obrigatório");
                formcontato.datanasc.focus();
                return false;
            }if (endereco == "") {
                alert("campo de endereço é obrigatório");
                formcontato.endereco.focus();
                return false;
            }if (numero == "") {
                alert("campo de numero é obrigatório");
                formcontato.numero.focus();
                return false;
            }if (bairro == "") {
                alert("campo de bairro é obrigatório");
                formcontato.bairro.focus();
                return false;
            }if (telefone == "") {
                alert("campo nome é obrigatório");
                formcontato.telefone.focus();
                return false;
            }
        }



    </script>
</head>
<body>
<!-- -->
<form name="formcontato" action="dadoscliente.php" id="form" method="POST" >
    <table border="1" align="center">

        <h1 align="center">Ficha de cadastro</h1>

        <tr><td>Nome: <input type="text" placeholder="Digite seu nome" id="nome" name="nome" ></td></tr>

        <tr><td>Sobrenome: <input type="text" placeholder="Digite seu sobrenome" id="snome" name="snome"></td></tr>

        <tr><td>Estado Civil: 
            <select name="estadociv">
                <option>Solteiro</option>
                <option>Casado</option>
                <option>Divorciado</option>
            </select></td></tr>

        <tr><td>Data de nascimento: <input type="date" id="datanasc" name="datanasc"></td></tr>

        <tr><td>Endereco: <input type="text" placeholder="Digite seu endereço" id="endereco" name="endereco"></td></tr>

        <tr><td>Numero: <input type="number" placeholder="Digite o numero da casa" id="numero" name="numero"></td></tr>

        <tr><td>Bairro: <input type="text" id="bairro" placeholder="Digite o bairro" name="bairro"></td></tr>

        <tr><td>Estado: 
            <select name="estado">
                <option>Sao Paulo</option>
                <option>Rio de Janeiro</option>
                <option>Santa Catarina</option>
            </select></td></tr>

        <tr><td>Telefone: <input type="tel" id="telefone" placeholder="(xx) xxxx-xxxx" name="telefone"></td></tr>

        <tr><td colspan="2" align="center"><input type="submit" value="Enviar" onclick="return validar_form()" id="enviar"></td></tr>


    </table>

</form>

</body>

</html>

TABLE WITH COMPLETED DATA:

<?php 

    session_start();
    include_once('conexao.php');
 ?>
 <!DOCTYPE html>
 <html>
 <head>

 <style type="text/css">

table, th, td{
border: 1px solid black;
 padding: 5px;
}

}
table {
border-spacing: 15px;
}

 </style>
 </head>
 <body>

<form>
    
    <table style="width: 100">
        <thead>
            <tr>
                <th>ID</th>
                <th>Nome</th>
                <th>Sobrenome</th>
                <th>Estado civil</th>
                <th>Data de nascimento</th>
                <th>Endereço</th>
                <th>Estado</th>
                <th>Telefone</th>

            </tr>
        </thead>
        <tbody>

            <?php

                $result_usuarios = " SELECT * FROM teste.cadastro";
                $resultado_usuarios = mysqli_query($con, $result_usuarios);
                while($row_usuario = mysqli_fetch_assoc($resultado_usuarios)){
                    echo 
                        "<tr>
                            <td><a href='http://127.0.0.1/teste/'>".$row_usuario["codigo"]."</td>
                            <td>".$row_usuario["nome"]. "</a></td>
                            <td>".$row_usuario["sobrenome"]."</td>
                            <td>".$row_usuario["estadociv"]."</td>
                            <td>".$row_usuario["datanasc"]."</td>
                            <td>".$row_usuario['endereco'] . ", " . $row_usuario['numero'] . " - " . $row_usuario['bairro'] . "</td>
                            <td>".$row_usuario["estado"]."</td>
                            <td>".$row_usuario["telefone"]."</td>
                        </tr>";

            
                }
            ?>  


        </tbody>


    </table>

</form>
 
 </body>
 </html>
  • makes the "test" page that has in your url read (can be request of the url for example) the ID, does the same select, but with where with the id and returns this data

  • Thank you so much for the tip, I managed to put the ID through the URL

1 answer

0


You’ve done a lot of the way. One of the most used ways is to send the ID via $_GET, and recover the data on the other screen, in your case the form screen. Your link would look like this:

<td><a href=http://127.0.0.1/teste/formulario.php?codigo=".$row_usuario["codigo"].">".$row_usuario["codigo"]."</td>

Or so if you are using friendly url

<td><a href='http://127.0.0.1/teste/".$row_usuario["codigo"]."/>".$row_usuario["codigo"]."</td>

On your page with the form you recover the data normally with a query in the database.

$codigo = $_GET['codigo'];
$result_usuarios = " SELECT * FROM teste.cadastro WHERE codigo='{$codigo}'";
$resultado_usuarios = mysqli_query($con, $result_usuarios);
$row_usuario = mysqli_fetch_assoc($resultado_usuarios);

<input type="text" name="nome" value="<?php echo $row_usuario['nome']; ">

EDIT

In that case you won’t need the while as you will only be returning one record, if your code field is a unique ID in your database.

  • 1

    Thank you so much! I made it work!

Browser other questions tagged

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