duandi6531 2017-03-31 20:39
浏览 29
已采纳

Laravel:更改文本框编辑的元素

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:

{{ 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()}}

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?

  • 写回答

1条回答 默认 最新

  • dongliang1654 2017-03-31 21:27
    关注

    first issue , you are using multi ID in your DOM document, which is totally wrong .

    <td><div id="txtHint">Name: {{ $name }}</div></td>
    <td><div id="txtHint">Price: {{ $price }}</div></td>
    

    must be as follows :

    <td><div id="nameHint">Name: {{ $name }}</div></td>
    <td><div id="priceHint">Price: {{ $price }}</div></td>
    

    then, in your php side you will need to echo some data , and to send multiple data the best approach by encode it into json , then decode that json from your client side and parse it into DOM elements using javascript.

    so , in your php code , modify it to be as follows :

    $price = $sql[0]->price;
    $name = $sql[0]->name;
    echo json_encode(array("price" => $price, "name" => $name));
    

    then from your client side , decode that json as follows :

    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'];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?