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?
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?
Share asked Apr 1, 2017 at 9:49 FeralheartFeralheart 1,9436 gold badges32 silver badges64 bronze badges 1- you cant apply onchange event to text input. it is for select tag.. use onblur. – f_i Commented Apr 1, 2017 at 10:10
1 Answer
Reset to default 4Try 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./docs/5.3/html
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744924044a4601317.html
评论列表(0条)