如何在表单输入字段中获取数据?

时间:2019-07-12 14:45:51

标签: javascript php ajax forms input

我正在尝试创建一个表单,以补充数据库中的数据。工作原理是这样的:客户输入公司编号,该表单在数据库中搜索是否存在该编号,如果存在,它将替换输入的“公司名称”和输入的“公司地址”中的值

我几乎做到了,问题是公司的名称和地址显示为文本。我希望他们``出现在输入中,我在做什么错了?

我只是使用for来做到这一点,我不知道如何正确地做到这一点。

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

在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];
                                                }             
                    }
                    ?>

2 个答案:

答案 0 :(得分:0)

在循环中,将所有值放入数组。 例如$myArray['address'] = 'my fabulous address'

之后,您可以像这样在字段中回显数组的值

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

答案 1 :(得分:0)

首先,将您的PHP代码更改为将PDOPrepared Statements结合使用。目前,您的代码可以接受SQL注入攻击!这是一个很好的使用方法教程:PHP PDO Prepared Statements Tutorial to Prevent SQL Injection

关于您的问题,在填充文本输入字段时,您需要将数据设置为属性value而不是innerHTML

document.getElementById("KonNaz").value = this.responseText;