How to fill in the inputs with the information returned from a query?

Asked

Viewed 1,405 times

-1

I’m testing the https://github.com/jansenfelipe/cnpj-gratis.

He returns to me after the query the following information:

array(21) { ["data_abertura"]=> string(10) "23/05/1979" ["razao_social"]=> 
string(10) "SADIA S.A." ["nome_fantasia"]=> string(8) "********" 
["cnae_principal"]=> string(8) "********" ["cnaes_secundario"]=> string(8) 
"********" ["natureza_juridica"]=> string(34) "205-4 - Sociedade Anônima 
Fechada" ["logradouro"]=> string(8) "********" ["numero"]=> string(8) 
"********" ["complemento"]=> string(8) "********" ["cep"]=> string(8) 
"********" ["bairro"]=> string(8) "********" ["cidade"]=> string(8) 
"********" ["uf"]=> string(2) "**" ["email"]=> string(27) "Delmir.Dal-
[email protected]" ["telefone"]=> string(13) "(49) 4443-000" 
["ente_federativo_responsavel"]=> string(5) "*****" ["situacao_cadastral"]=> 
string(7) "BAIXADA" ["situacao_cadastral_data"]=> string(10) "31/12/2012" 
["motivo_situacao_cadastral"]=> string(12) "INCORPORACAO" 
["situacao_especial"]=> string(8) "********" ["situacao_especial_data"]=> 
string(8) "********" }

This is the php code:

<?php

require_once '../vendor/autoload.php';

use JansenFelipe\CnpjGratis\CnpjGratis;


if(isset($_POST['captcha']) && isset($_POST['cookie']) && 
 isset($_POST['cnpj'])){
$dados = CnpjGratis::consulta($_POST['cnpj'], $_POST['captcha'], 
$_POST['cookie']);
var_dump($dados);
die;
}else
$params = CnpjGratis::getParams();
?>

<img src="data:image/png;base64,<?php echo $params['captchaBase64'] ?>" />

<form method="POST">
<input type="hidden" name="cookie" value="<?php echo $params['cookie'] ?>" 
/>

<input type="text" name="captcha" placeholder="Captcha" />
<input type="text" name="cnpj" placeholder="CNPJ" />

<button type="submit">Consultar</button>
</form>

How can I get the information and self-prefill an input with it?

  • You want to fill only 1 input with all the information or for each information a new input?

1 answer

1


Just you use the foreach. It will sweep the array $dados and insert a input in each element.

<?php

require_once '../vendor/autoload.php';

use JansenFelipe\CnpjGratis\CnpjGratis;


if(isset($_POST['captcha']) && isset($_POST['cookie']) && isset($_POST['cnpj'])){
    $dados = CnpjGratis::consulta($_POST['cnpj'], $_POST['captcha'], $_POST['cookie']);

    // abaixo eu seleciono os dados e incluo em um input um por vez
    foreach($dados as $chave => $info){
?>

    <input type="text" value="<?php echo $info; ?>"/>

<?php

    } // fechamento do foreach

}else
    $params = CnpjGratis::getParams();
?>

<img src="data:image/png;base64,<?php echo $params['captchaBase64']; ?>" />

<form method="POST">
<input type="hidden" name="cookie" value="<?php echo $params['cookie']; ?>" />

<input type="text" name="captcha" placeholder="Captcha" />
<input type="text" name="cnpj" placeholder="CNPJ" />

<button type="submit">Consultar</button>
</form>
  • Thank you, thank you very much

Browser other questions tagged

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