Laravel:更改文本框编辑的元素

First Laravel Project. I want to make a form where if I write something in a textbock (for example a barcode) the code searches in a database and prints out a value (for example the price)

My code is so far:

The view:

{{ Form::open(array('url' => '/product/$barcode'))}}
<script>
function showProduct(str) {
    if (str == "") {
        document.getElementById("txtHint").innerHTML = "";
        return;
    } else {
        if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else {
            // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
                document.getElementById("txtHint").innerHTML = this.responseText;
            }
        };
        xmlhttp.open("GET","script/getproduct.php?q="+str,true);
        xmlhttp.send();
    }
}
</script>

//If I don't set the @vars first I get "unknown variable" error
<?php $name=0; $price=0; ?>
<table>
<tr>
<td>{{Form::text('barcode', null, ['onchange'=>'showProduct(this.value)'])}}</td>
<td><div id="txtHint">Name: {{ $name }}</div></td>
<td><div id="txtHint">Price: {{ $price }}</div></td>
</tr>
</table>
{{Form::close()}}

The showproduct.php:

<?php
$q = intval($_GET['q']);

$sql = DB::select('select * from inventory where barcode = ? LIMIT 1', [$q]);

$price = $sql[0]->price;
$name = $sql[0]->name;
?>

But when I put a barcode to the textfield I still got 0 in the fields. What I did wrong?

first issue , you are using multi ID in your DOM document, which is totally wrong .

<td><div id="txtHint">Name: {{ $name }}</div></td>
<td><div id="txtHint">Price: {{ $price }}</div></td>

must be as follows :

<td><div id="nameHint">Name: {{ $name }}</div></td>
<td><div id="priceHint">Price: {{ $price }}</div></td>

then, in your php side you will need to echo some data , and to send multiple data the best approach by encode it into json , then decode that json from your client side and parse it into DOM elements using javascript.

so , in your php code , modify it to be as follows :

$price = $sql[0]->price;
$name = $sql[0]->name;
echo json_encode(array("price" => $price, "name" => $name));

then from your client side , decode that json as follows :

if (this.readyState == 4 && this.status == 200) {
    var jsonObject = JSON.parse(this.responseText);
    document.getElementById("priceHint").innerHTML = jsonObject['price'];
    document.getElementById("nameHint").innerHTML = jsonObject['name'];
}