dotws86260 2016-08-16 16:46
浏览 138

Laravel(初学者) - 显示下拉列表中选定行的数据

This is Controller.php

public function dropdown($id)
  {

    $stations = Station::lists('station_name','id');
    return view('stations.show')->withStation($station);
    return view('stations.dropdown',compact('stations', 'selectedStation','selectedStation'));

  }

This is the view.php

{{ Form::select('station',$stations,null,['class' => 'form-control']) }} 
            <br>
<?php
        if(isset($_GET['submit'])){


            $selectedStation = $_GET['station'];
            echo $selectedStation;echo "<br>";
        }
        else
        {
          echo "not working";
        }
    ?>

corresponding Database table. database image

This gives me a drop-down like in the below image.enter image description here

When a station is selected it displays only "id" of the selected station. How to retrieve other columns of the selected station ?.

  • 写回答

2条回答 默认 最新

  • dpir3950 2016-08-16 16:54
    关注

    Many ways that you can tackle this and I have also made some assumptions. Your list (it would seem) only contains the name & the id.

    I assume that you don't want to reload the page when you get the information for the specific item that you have chosen in the dropdown.

    Here are the first 2 options that come to mind.

    1.) AJAX - Use javascript framework like jQuery and monitor change() for the select, on change get the id and fire it to another route in Laravel which takes the ID and returns some more information in JSON. You can then use JS to display this to your users as you please.

    2.) Preloaded JSON - Write new DB Query that gets all the information that you want and store it in JSON format inside some <script> tags. It wont be viewable to the user. Then you can use jQuery or other JS framework to grab that data again on change()

    Part 2 will be faster, and if you have got 100,000s records i would advise that for perf.

    Apologies if I totally misunderstood the question, it seems like you need a JS solution rather than Laravel/PHP solution. (Without a bad UX) if you did wan't more data you would need to post it and the fetch data from DB on new request.

    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算