How to pass a value from a table column to a form on another PHP page via HTTP?

Asked

Viewed 33 times

-2

I would like to pass the value of exibir_colunas['cd_cliente'] page List customers by the tag <a> (recalling that the #atu_cli serves as anchor for the form as it is an id of the update form).

echo '<td>'.$exibir_colunas['cd_cliente'].'</td>';
echo '<td>'."<a href='/web/form_crud/form_update_cliente.php/#atu_cli'> Atualizar </a>".'</td>';

And on the form page Update client the value of exibir_colunas['cd_cliente'] be received by the camp <select> for value="<?php echo $exibir_colunas['cd_cliente']; ?>.

<?php

        // Mostrar todos os erros do php
        ini_set('display_errors', '1');
        ini_set('display_startup_errors', '1');
        error_reporting(E_ALL);

        $exibir_colunas['cd_cliente'] = $_GET['cd_cliente'];

        try {

            $seleciona_nomes = $conexao->query("SELECT cd_cliente, nome FROM cliente ORDER BY cd_cliente");
            $resultado_selecao = $seleciona_nomes->fetchAll();

        } catch (PDOException $falha_selecao) {
            echo "A listagem de dados não foi feita".$falha_selecao->getMessage();
            die;
        } catch (Exception $falha) {
            echo "Erro não característico do PDO".$falha->getMessage();
            die;
        }
    ?>
 
<p> ID cliente:
    <select onclick="buscaDados()" name="cd_cliente" id="cd_cliente" value="<?php echo $exibir_colunas['cd_cliente'] ?>">
            <option value=""> Nenhum </option>
            <?php foreach($resultado_selecao as $valor): ?>
                    <option value="<?= $valor['cd_cliente'] ?>"><?= $valor['nome'] ?></option>
            <?php endforeach ?>
    </select>
</p>

1 answer

1

Introducing

Right, on your question it is important to understand some concepts to understand how data is trafficked via HTTP. HTTP has some methods, among them the most famous and most common are GET and POST.

  • GET is usually used to get data, you use it when you click on a link on a website or when you type an address in the bar of browser.

  • The POST is usually used to send data, you use it to submit forms for example.

Another HTTP concept is Request and Response. A request is the act of requesting or sending something to the server, while a response is what the server returns. They may or may not have a body, but they will always have a headline, which is the part that talks about whether or not it was successful.

What you mention of wanting to send information by href can be understood as sending information by Query String.

When submitting a request you can add the URL the ? character, e.g..

http://exemplo.com?parametro1=valor1&parametro2=valor2

In this example in our URL if it is used as a link when clicking is:

  • A GET request is sent to http://exemplo.com
  • In the string query 2 parameters are sent (parametro1 and parametro2).

Assuming you are using PHP you would get these values using the $_GET magic variable.

For that you would have a code like:

<?php

$valor1 = $_GET['parametro1'];
$valor2 = $_GET['parametro2'];

Applying to your case

In your case what you would do would be something like

Listing

<a href='/web/form_crud/form_update_cliente.php/?cd_cliente=<?= $exibir_colunas['cd_cliente']; ?>#atu_cli'> Atualizar </a>

Update page

<?php
//... seu código, em algum momento inicializa $exibir_colunas como array.
$exibir_colunas['cd_cliente'] = $_GET['cd_cliente'];
//... mais código
?>
<p> ID cliente: <select onclick="buscaDados()" name="cd_cliente" id="cd_cliente" required="" value="<?php echo $exibir_colunas['cd_cliente']; ?>"> </p>

Considerations.

I hope this helped to understand how this passage occurs in PHP and has solved your question.

  • I haven’t been able to.

  • I think it is in the update form, because the listing is correct.

Browser other questions tagged

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