【问题标题】:How to use a variable as "value" attribute in <option> tag如何在 <option> 标签中使用变量作为“值”属性
【发布时间】:2018-06-04 13:12:50
【问题描述】:

I'm currently using Laravel and I want to have a dropdown box display category names from my database, but when selected the value it returns to the server should be a numerical value of the name selected which is also stored in the database .

像这样:

@foreach($categories as $category)
   <option name="pid" value="$category->id" id="cname">{{$category->name}}</option>
@endforeach

值从我的控制器传递到 Blade 模板。如您所见,我希望它在下拉列表中显示类别名称(这很好),但是一旦选择并提交,我希望返回类别 ID。由于“值”属性返回值,我希望变量$category-&gt;id 作为“值”。我想在不使用 JavaScript 或 jQuery 的情况下做到这一点。仅使用 PHP。我将如何实现这一点,甚至有可能吗?

当我尝试使用 value="{{$category-&gt;id}}" 时,我收到 Laravel 错误:

违反完整性约束:1048 列 'parent_id' 不能为空

所以它似乎没有提交下拉列表的值。

这是我目前在 Blade 模板中使用的代码:

<form class="" action="{{route('createsubcategorypost')}}" method="post">
  <div class="form-group">
    <label for="cname">Sub-Category name:</label>
    <input type="text" name="cname" value="" placeholder="Sub-Category Name" id="cname" class="form-control">
  </div>
    <div class="form-group">
        <label for="pid">Parent ID</label>
        <select class="form-control">
          @foreach($categories as $category)
            <option name="pid" value="{{$category->id}}" id="pid">{{$category->name}}</option>
          @endforeach
        </select>
  </div>
  <div class="form-group">
    <center>
      <button type="submit" name="button" class="btn btn-success">Create Sub-Category</button>
    </center>
  </div>
{{csrf_field()}}
</form>

这是我的控制器中的代码:

public function CreateSubCategoryPost(Request $request){
        if ($request->cname == null) {
          session()->flash('errormessage',' Invalid sub-category name.');
          return redirect()->back();
        }
        $scc = SubCategory::where('name',$request->cname)->first();
        if ($scc !== null) {
          session()->flash('errormessage',' Sub-category with that name already exists.');
          return redirect()->back();
        }
        $subCategory = new SubCategory;
        $subCategory->name = $request->cname;
        $subCategory->uniqueid = 'SCA'.str_random(28);
        $subCategory->slug = str_slug($request->cname,'-');
        $subCategory->parent_id = $request->pid;
        $subCategory->save();
        return redirect()->route('categories');
    }

【问题讨论】:

    标签: php html laravel bootstrap-4 blade


    【解决方案1】:

    与您在选项标签内显示类别名称的方式相同:

    @foreach($categories as $category)
       <option name="pid" value="{{$category->id}}" id="cname">{{$category->name}}</option>
    @endforeach
    

    【讨论】:

    • 我实际上已经尝试过这种方式,但是,在提交表单时,我收到 Laravel 错误:“完整性约束违规:1048 列'parent_id'不能为空”。所以它似乎没有提交“$category->id”值(这是子类别的父 ID)。我已经用我当前使用的代码更新了我的原始帖子。
    • 它给你这个错误是因为你需要给
    • 哇,简直不敢相信我错过了这么简单的事情。这样就彻底解决了。它现在正在工作。非常感谢!
    【解决方案2】:

    你从 Laravel 中的数据库调用的所有内容都应该具有 {{$category-&gt;someName}} 格式,无论你将它放在视图中的什么位置。在您的情况下,您需要将 $category-&gt;id 括在 {{ }}

    【讨论】:

    • 我收到 Laravel 错误:“违反完整性约束:1048 列 'parent_id' 不能为空”。请查看我更新的帖子以及更多代码详细信息。谢谢。
    • 这会从表单获取数据以发布到数据库$model_instance -&gt;db_column = $request-&gt;input-name; 这会保存数据$model_instance-&gt;save(); 这会初始化控制器以从数据库中提取数据$model_instance = model_name::find($id); 这会告诉控制器您想要哪个视图使用模型中的数据return view('folder.show')-&gt;with('any_name', $model_instance); 最后这就是你在视图中使用的{{ $any_name-&gt;db-column }} 总而言之,在你的情况下 - {{ $subCategory-&gt;db-column }}
    • 感谢您的所有帮助。结果我只需要给
    猜你喜欢
    • 2011-05-26
    • 2019-10-01
    • 1970-01-01
    • 2017-09-17
    • 2020-04-07
    • 2014-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多