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>");
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!.
– G. Falcão