Pass variable value through a button

Asked

Viewed 1,039 times

1

There is a way to pass the value of a variable recovered from a database search to a button, which will redirect the user to another page, where a new database search will be done using the variable value?

The code below is used for the first query in the database and to show in table form some data. Inside it is the button that will redirect to another page, where I want to use the value of the variable email to do another search in the database.

<?php
              require_once('conecta.php');
              $pasta = "imagens/";
              $objDb = new db();
              $link = $objDb->conecta_mysql();
              $consulta = mysqli_query($link, "SELECT * FROM voluntarios WHERE grupo = '' order by nome");
              while ($resultado = mysqli_fetch_array($consulta)) {
                $link = $pasta . $resultado["foto"];

                echo '<img src='.$link.' width="100px" height="100px"></br>
                <center><table border=1>
                  <thead>
                    <tr><td colspan="2" align="center">   '.$resultado["nome"].'   </td></tr>
                  </thead>
                  <tbody>
                    <tr>
                      <td align="center">&nbsp E-mail &nbsp</td>
                      <td align="center">&nbsp '.$resultado["email"].' &nbsp</td>
                    </tr>
                    <tr>
                      <td align="center">&nbsp Data de nascimento &nbsp</td>
                      <td align="center">&nbsp '.$resultado["data"].' &nbsp</td>
                    </tr>
                    <tr>
                      <td align="center">&nbsp CPF &nbsp</td>
                      <td align="center">&nbsp '.$resultado["cpf"].' &nbsp</td>
                    </tr>
                    <tr>
                      <td align="center">&nbsp Contato &nbsp</td>
                      <td align="center">&nbsp '.$resultado["tel"].' / '.$resultado["cel"].' &nbsp</td>
                    </tr>
                    <tr>
                      <td align="center">&nbsp Endereço &nbsp</td>
                      <td align="center">&nbsp CEP: '.$resultado["cep"].' / Número: '.$resultado["numero"].' &nbsp</td>
                    </tr>
                    <tr>
                      <td align="center">&nbsp Instituição &nbsp</td>
                      <td align="center">&nbsp '.$resultado["instituicao"].' &nbsp</td>
                    </tr>
                    <tr>
                      <td align="center">&nbsp Nível  &nbsp</td>
                      <td align="center">&nbsp '.$resultado["nivel"].' &nbsp</td>
                    </tr>
                    <tr>
                      <td align="center">&nbsp Curso &nbsp</td>
                      <td align="center">&nbsp '.$resultado["curso"].' &nbsp</td>
                    </tr>
                    <tr>
                      <td align="center">&nbsp Área &nbsp</td>
                      <td align="center">&nbsp '.$resultado["area"].' &nbsp</td>
                    </tr>
                    <tr>
                      <td align="center">&nbsp Turno disponível &nbsp</td>
                      <td align="center">&nbsp '.$resultado["turno"].' &nbsp</td>
                    </tr>
                    <tr>
                      <td align="center">&nbsp Horário &nbsp</td>
                      <td align="center">&nbsp '.$resultado["horario"].' &nbsp</td>
                    </tr>
                    <tr>
                      <td align="center">&nbsp Habilidades extras &nbsp</td>
                      <td align="center">&nbsp '.$resultado["habilidades"].' &nbsp</td>
                    </tr>
                    <tr>
                      <td align="center">&nbsp Ministrará palestras &nbsp</td>
                      <td align="center">&nbsp '.$resultado["palestras"].' &nbsp</td>
                    </tr>
                    <tr>
                      <td align="center">&nbsp Cidade &nbsp</td>
                      <td align="center">&nbsp '.$resultado["cidade2"].' &nbsp</td>
                    </tr>
                  </tbody>
                </table></center>
                <center><a href="vol.php" class="btn btn-custom btn-roxo ">Acessar</a></center></br></br>
                ';
            } 
            ?>
  • Have you tried $_SESSION ? Try to show us your problem by putting your code here, so the community can give an answer that can specifically solve your problem.

  • @Rickpariz I didn’t try with $_SESSION, but how can it happen to have more than one result, would it be okay to use it? I added the code to the question!

  • 1

    One of the alternatives is to pass the email through the url, it would look something like vol.php? email=$result['email].

  • @Rickpariz I tried to do this way, it passes the value of the variable email, however I could not recover this value on the other page, I used the same friend below said $email = filter_input(INPUT_GET, 'email', FILTER_VALIDATE_EMAIL);

1 answer

1


Just pass the variable via URL.

<a href="vol.php?email=' . $resultado["email"] . '" class="btn btn-custom btn-roxo ">Acessar</a>

To get the variable in the script that will process the call, just use:

$email = $_GET['email'];

or if you want a little more security

$email = filter_input(INPUT_GET, 'email', FILTER_VALIDATE_EMAIL);
  • Doing so the right way, he passes the email, but I put an echo to show the variable $email but nothing appears. I used the $email = filter_input(INPUT_GET, 'email', FILTER_VALIDATE_EMAIL);

  • It worked, I had forgotten to put email=. Thank you so much for your help!!!

  • Glad it all worked out =]

Browser other questions tagged

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