How to upload information from a BD to the form?

Asked

Viewed 253 times

3

Hello, everyone and good afternoon! I will explain my problem and point out that I would like you to solve only with PHP, but almost sure I need another language.

I would like in a form, in a tag have options with company names, for example: Microsoft, Apple, Google, etc. And from the choice of this company a form below pull from the BD the information and self-prefill the form data.

Could you enlighten me on how to do this? I searched the internet and most cite jQuery to do such action, but unfortunately I have no knowledge in js.

No caso, ao invés de ser "Incluir nova empresa" será o nome da empresa e os outros campos serão preenchidos automaticamente.

In case, instead of being "Include new company" it will be the company name and the other fields will be filled automatically.

1 answer

1


1st Step:

_form.php file

Script

function loadDoc() {
   var empresa = document.getElementById('empresa').value;
   var xhttp = new XMLHttpRequest();
   xhttp.onreadystatechange = function() {
     if (this.readyState == 4 && this.status == 200) {
       document.getElementById("result").innerHTML =
       this.responseText;
     }
   };
     xhttp.open("GET", "_form2.php?empresa="+empresa, true);
     xhttp.send();

 }

HTML

<form>
  <select name="empresa" size="5" id="empresa">
    <option value="Microsoft">Microsoft</option>
    <option value="Apple">Apple</option>
    <option value="Google">Google</option>
  </select>

 <button type='button' onclick="loadDoc()">Submit</button>
 </form>
 <!-- na div abaixo será retornado o resultado da consulta -->
 <div id='result'>
 </div>

second step:

_form2.php file

$empresa=$_GET["empresa"];
//bonus 
$empresa=mb_convert_encoding($empresa, "UTF-8");

    $link = new mysqli ("localhost", "USUARIO", "SENHA", "DB");

    if($link->connect_errno){
         echo"Deu ruim";
         exit();
    }


        $sqli = ("SELECT empresa,razao,cnpj,tel FROM stabela WHERE empresa = '$empresa'");
        $resultado_pedido = mysqli_query($link,$sqli);

        if (mysqli_num_rows($resultado_pedido) > 0) {
                while($row = mysqli_fetch_assoc($resultado_pedido)) {
                    $empresa=$row["empresa"];
                    $razao=$row["razao"];
                    $cnpj=$row["cnpj"];
                    $tel=$row["tel"];
                }
            }

   mysqli_close($link);

   echo ("<form>");
   echo "<br>";
   echo ("<input type=\"text\" name=\"empr\" id=\"empr\" value=\"".$empresa."\">");
   echo "<br>";
   echo ("<input type=\"text\" name=\"empr\" id=\"empr\" value=\"".$razao."\">");
   echo "<br>";
   echo ("<input type=\"text\" name=\"empr\" id=\"empr\" value=\"".$cnpj."\">");
   echo "<br>";
   echo ("<input type=\"text\" name=\"empr\" id=\"empr\" value=\"".$tel."\">");
   echo ("</form>");
  • 1

    Thank you very much, Leo Caracciolo, I modified the code a little and I will post here, but as I did not know much of javascript, facilitated the creation of the code!.

Browser other questions tagged

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