How to load information from a DB into the form?

3

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

I would like in a form, in a tag to have options with names of companies, for example: Microsoft, Apple, Google, etc. And from the choice of this company a form below pull the BD's information and auto fill in the form data.

Could you tell me how to do this? I searched the internet and most quotes jQuery to do such an action, but unfortunately I have no knowledge of js.

Nocase,insteadofbeing"Include new company" will be the company name and the other fields will be filled automatically.

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

    
asked by anonymous 06.04.2017 / 23:34

1 answer

1

Step 1:

  

_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>

Step 2:

  

_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>");
    
07.04.2017 / 02:22