Laravel:在编辑文本框时更改元素
Laravel: Changing elements on textbox edit
第一个 Laravel 项目。
我想制作一个表格,如果我在文本框(例如条形码)中写一些东西,代码会在数据库中搜索并打印出一个值(例如价格)
到目前为止我的代码是:
观点:
{{ 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()}}
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;
?>
但是当我将条形码放入文本字段时,我在字段中得到的仍然是 0。我做错了什么?
第一期,您在 DOM 文档中使用了多 ID,这是完全错误的。
<td><div id="txtHint">Name: {{ $name }}</div></td>
<td><div id="txtHint">Price: {{ $price }}</div></td>
必须如下:
<td><div id="nameHint">Name: {{ $name }}</div></td>
<td><div id="priceHint">Price: {{ $price }}</div></td>
然后,在您的 php 端,您将需要 echo
一些数据,并且发送多个数据的最佳方法是将其编码为 json,然后解码 json 从您的客户端并使用 javascript.
将其解析为 DOM 元素
所以,在你的php代码中,修改如下:
$price = $sql[0]->price;
$name = $sql[0]->name;
echo json_encode(array("price" => $price, "name" => $name));
然后从您的客户端,解码 json 如下:
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'];
}
第一个 Laravel 项目。 我想制作一个表格,如果我在文本框(例如条形码)中写一些东西,代码会在数据库中搜索并打印出一个值(例如价格)
到目前为止我的代码是:
观点:
{{ 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()}}
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;
?>
但是当我将条形码放入文本字段时,我在字段中得到的仍然是 0。我做错了什么?
第一期,您在 DOM 文档中使用了多 ID,这是完全错误的。
<td><div id="txtHint">Name: {{ $name }}</div></td>
<td><div id="txtHint">Price: {{ $price }}</div></td>
必须如下:
<td><div id="nameHint">Name: {{ $name }}</div></td>
<td><div id="priceHint">Price: {{ $price }}</div></td>
然后,在您的 php 端,您将需要 echo
一些数据,并且发送多个数据的最佳方法是将其编码为 json,然后解码 json 从您的客户端并使用 javascript.
所以,在你的php代码中,修改如下:
$price = $sql[0]->price;
$name = $sql[0]->name;
echo json_encode(array("price" => $price, "name" => $name));
然后从您的客户端,解码 json 如下:
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'];
}