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

报告相同问题?

悬赏问题

  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改