Feralheart
Feralheart

Reputation: 1928

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>
{{ Form::open(array('url' => '/test'))}}
<table>
<tr>
<td>{{Form::text('barcode', null,['onchange'=>"showProduct(this.value)"])}}</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?

Upvotes: 1

Views: 8397

Answers (1)

abSiddique
abSiddique

Reputation: 12343

Try this

{!! Form::text('barcode', null,['onchange'=>"showProduct(this.value);"]) !!}

Also start form as below

{!! Form::open(['url' => 'foo/bar']) !!}
//
{!! Form::close() !!}

For more follow https://laravelcollective.com/docs/5.3/html

Upvotes: 4

Related Questions