Friday, March 31, 2017

Laravel: Changing elements on textbox edit

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:


<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></td>
<td><div id="txtHint">Name: </div></td>
<td><div id="txtHint">Price: </div></td>
</tr>
</table>


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?



via Feralheart

Advertisement