1

I am trying to create a form that will be complemented with data from the database. This is how it works: the customer enters the company number, and the form searches in the database if the number exists, if yes, it replaces value in the input "Company name" and in the input "Company address"

I almost did it, the problem is that the name and address of the company show up, however, as text. I would like them to ``show up in inputs, what am I doing wrong?

I just use the for do this, I don't have any idea how to do it correctly.

<html>
    <head>
        <meta charset="UTF-8">
<link href="https://fonts.googleapis.com/css?family=Poppins&display=swap" rel="stylesheet"> 
<link rel="stylesheet" href="lib/jquery-ui.css" />
<script src="lib/jquery-1.8.3.js"></script>
<script src="lib/jquery-ui.js"></script>
<script src="lib/jquery.ui.datepicker-pl.js"></script>
<script type='text/javascript' src='lib/jquery.bgiframe.min.js'></script>
<script type='text/javascript' src='lib/jquery.ajaxQueue.js'></script>
<script type='text/javascript' src='lib/thickbox-compressed.js'></script>
<script type='text/javascript' src='lib/jquery.autocomplete.js'></script>
<link rel="stylesheet" type="text/css" href="lib/jquery.autocomplete.css" />
<link rel="stylesheet" type="text/css" href="lib/lib/thickbox.css" />
<script>onload="window.parent.parent.scrollTo(0,0)"</script>

//There's the script to show date from database

    <script>
function showHint(str) {
    if (str.length == 0) { 
        document.getElementById("KonNaz").innerHTML = "";
        return;
    } else {
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
                document.getElementById("KonNaz").innerHTML = this.responseText;
            }
        };
        xmlhttp.open("GET", "gethint.php?q=" + str, true);
        xmlhttp.send();
    }
    {
         if (str.length == 0) { 
        document.getElementById("KonAdr").innerHTML = "";
        return;
    } else {
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
                document.getElementById("KonAdr").innerHTML = this.responseText;
            }
        };
        xmlhttp.open("GET", "gethint2.php?q=" + str, true);
        xmlhttp.send();
    }

    }
}
</script>



</head>
<body>


//Theres a form

<form action="baza_danych_faktury.php" method="post" enctype="multipart/form-data" class="form-inline"> 

Kontrahent NIP (Company Number)</br>

    <form>
<div id="KonNaz"><input type="text" placeholder="np. 7740001454" name="KonNip" class="inputspecial" required onkeyup="showHint(this.value)"> </div> </br>
    </form>

Kontrahent nazwa (Company Name)</br>

<input type="text" placeholder="np. Polski Koncern Naftowy Orlen" name="KonNaz" id="KonNaz" class="inputspecial" required> </div> </br>

Kontrahent adres (Company Addres)</br>

<div id="KonAdr"><input type="text" placeholder="np. Katowice 40-847 UL. BOCHEŃSKIEGO 99" name="KonAdr" class="inputspecial" required> </div> </br>

(...)

</form>

in gethint.php:

<?php 
                    $q = $_REQUEST["q"];
                    $dbhost = 'localhost';     
                    $dblogin = 'login';
                    $dbpass = 'password';
                    $dbbase = 'database_name';
                    mysql_connect($dbhost,$dblogin,$dbpass);
                    mysql_select_db($dbbase) or die("Błąd przy wyborze bazy danych");
                    mysql_query("SET CHARACTER SET UTF8");
                    $wynik = mysql_query("SELECT * FROM evdb_nip WHERE NIP = '$q' ")
                    or die('Błąd zapytania'); 
                    if(mysql_num_rows($wynik) > 0) { 
                     while($r = mysql_fetch_assoc($wynik)) {
                         echo $r[Nazwa_Kontrahenta];
                                                }             
                    }
                    ?>
Sebastian
  • 43
  • 6

2 Answers2

0

In your loop while, put all values in array. For example $myArray['address'] = 'my fabulous address'

After that, you can echo values of array in field like this

<input type="text" placeholder="np. Katowice 40-847 UL. BOCHEŃSKIEGO 99" name="KonAdr" class="inputspecial" value="<?= $myArray['address'] ?>" required>
jobouille
  • 49
  • 2
0

First of all, change your PHP code to use PDO with Prepared Statements. Your code is, at this moment, open to receive SQL Injection attacks! Here is a good tutorial on how to use it: PHP PDO Prepared Statements Tutorial to Prevent SQL Injection

Regarding your question, when filling a text input field you need to set the data to the property value instead of innerHTML:

document.getElementById("KonNaz").value = this.responseText;
Caconde
  • 4,177
  • 7
  • 35
  • 32
  • Yes! It works, but I dont know why I cant send the form right now ... (?) When I click "send" just nothing happens – Sebastian Jul 12 '19 at 17:51
  • If you check the console in your browser's dev tools it shows any javascript errors? Or some error message is returned from google API? – Caconde Jul 12 '19 at 18:00
  • Just changing from `innerHTML` to `value` should not be enough to make your form stop working. – Caconde Jul 12 '19 at 18:04
  • 1
    Okay, I just have a second form (form in form) when I remove it, that works perfectly. Thank you! – Sebastian Jul 17 '19 at 13:54