Saturday, April 1, 2017

Laravel: onchange on Form::text

First Laravel Project. I want to run a script whenever I change the input in my Form::text. So far my code looks like this:

The view:

@extends('layouts.master')
@section('title', 'Onchange test')
@section('main')
@parent
<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();
        }
        xmlhttp.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
                var jsonObject = JSON.parse(this.responseText);                document.getElementById("nameHint").innerHTML = jsonObject['name'];
}
        };
        xmlhttp.open("GET","script/getproduct.php?q="+str,true);
        xmlhttp.send();
    }
}
</script>

<table>
<tr>
<td></td>
<td>
<td><div id="nameHint">Test</div></td>
</tr>
</table>
@endsection

The php

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

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

$test = $name;
echo json_encode(array("name"=> $name));
?>

But it's not refreshing (it shows only the deffault "Test") What did I wrong?



via Feralheart

Advertisement