【问题标题】:Problem of recovery of an element selected in a select恢复select中选择的元素的问题
【发布时间】:2022-11-12 16:55:57
【问题描述】:
Hello, I am a beginner in laravel. I want to implement a feature that I detail below:

I want to use laravel 8 and php :
 1/ Select a part of the data in a table (via a select)
        - how to get the id selected in the select
 2/ Copy the data and insert it in another table (via an add button)
      - how to recover the id present in the url of the page
 3/ Display the data inserted in a table
    - no problem

`

<select name="id_segment" class="form-control">


                            <?php
                            
                            $req1 = DB::select("SELECT id_segment, nom_segment FROM `segements` ");
foreach ($req1 as $re1 => $r1) {
 ?>
                            <option name="id_segment" value="{{ $r1->id_segment }}"> {{ $r1->id_segment }}
                                {{ $r1->nom_segment }}</option>
                            <?php
 } ?>
                        </select>

                    </div>
                </div>


                <div class="col-xs-12 col-sm-12 col-md-12 text-center">
                    <button type="submit" class="btn btn-primary" id="AjoutSegment">Ajouter</button>
                    <a class="btn btn-primary" href="{{ route('groupecampagnes.index') }}"> Back</a>
                </div>

`

我已经在制作 var_dump 时尝试过使用 $_POST 和 $_GET,但我看到有些人说它不起作用,它给了我一个错误,显示我试图准确恢复的 id,而它应该只是在页面上显示给我看我是否能很好地和真实地恢复它。 var_dump 不存在于代码中,因为我同时删除了它。在我尝试了在网上找到的各种解决方案之后,这些解决方案让我将其中一些与 std 类对象有关的错误显示为错误。

【问题讨论】:

  • 请阅读 markdown guide 并解决你的问题,它在当前状态下是不可读的。

标签: php sql laravel-8


【解决方案1】:

假设您的代码位于名为 page 的文件中并存储在视图目录 (resources/views/page.blade.php) 中,您可以转到控制器并将所需的数据传递给您的视图,如下所示:

$segments = DB::select("SELECT id_segment, nom_segment FROM `segements` ")->get();

return view('page', compact('segments'))

然后你可以在你的视图中使用它:

<select name="id_segment" class="form-control">
    @foreach ($segments as $r1)
        <option name="id_segment" value="{{ $r1->id_segment }}"> {{ $r1->id_segment }} {{ $r1->nom_segment }}</option>
    @endforeach
</select>

【讨论】:

    猜你喜欢
    • 2014-01-22
    • 1970-01-01
    • 2019-05-30
    • 1970-01-01
    • 2012-12-10
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 2016-04-03
    相关资源
    最近更新 更多