A
A
assx642017-02-01 22:17:22
Yii
assx64, 2017-02-01 22:17:22

How to make a linked list in Yii2 with the output of a row from the database?

<div class="row">
    <div class="col-md-4 vcenter">
        <?= $form->field($model, 'type_task_filter')->dropDownList(['city1'=>'city1','city2'=>city2'], ['prompt'=>'']); ?>
    </div>
</div>
<div class="row">
    <div class="col-md-4 vcenter">
        <?= $form->field($model, 'type_task_filter')-> ?  ?>
    </div>
</div>

Actually what is the question: When you select a drop-down list, it opens tied to it and displays the value from the database cell. For example, select 'city1' and a new field opens (appears) with data from the database cell. I don’t understand, is this possible with Yii2 methods? Please tell me the solution, I will be very grateful.
On native php + ajax, it would look like this: one file, *.php: create a form, create a table, set
<select id="cityvm" onChange="change_city()">
        <option></option>
<?php
  $res=mysqli_query($link,"select * from cities");
  while($row=mysqli_fetch_array($res)) 
{
?>
  <option value="<?php echo $row["id"]; ?>"><?php echo $row["name"]; ?></option>
<?php
}
?>
     </select>
Затем столбец для вывода строки из бд, которому задаем <div id="street">
Пишем js:
function change_city()
{
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.open("GET", "ajax.php?city="+document.getElementById("cityvm").value, false);
    xmlhttp.send(null);
    document.getElementById("street").innerHTML=xmlhttp.responseText;

}

И в другом файле ajax.php:
if($city !=""){
    $res = mysqli_query($link, "SELECT * FROM streets WHERE city_id = $city");
    while ($row = mysqli_fetch_array($res))
    {
        echo "<option value='$row[id]' selected>";
        echo $row["name"];
    }
}

И выглядело бы так:
ee6e9cd9da0d47988c235d0a93e2db06.jpg

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Максим Тимофеев, 2017-02-01
@webinar Куратор тега Yii

Ловим в js смену в select, отправляем ajax на сервер, там делаем выборку, возвращаем json (или готовый view), вставляем в DOM.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question