如何在 <option> 标签中使用变量作为 "value" 属性
How to use a variable as "value" attribute in <option> tag
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选择的名称也存储在数据库中。
像这样:
@foreach($categories as $category)
<option name="pid" value="$category->id" id="cname">{{$category->name}}</option>
@endforeach
这些值从我的控制器传递到 Blade 模板。如您所见,我希望它在下拉列表中显示类别名称(这很好),但是一旦选择并提交,我希望返回类别 ID。由于 "value" 属性 returns 的值,我想将变量 $category->id
作为 "value"。我想在不使用 JavaScript 或 jQuery 的情况下执行此操作。只有 PHP。我将如何着手完成这个,甚至可能吗?
当我尝试使用 value="{{$category->id}}"
时,出现 Laravel 错误:
Integrity constraint violation: 1048 Column 'parent_id' cannot be null
所以它似乎没有提交下拉列表的值。
这是我目前在我的 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');
}
与在选项标签内显示类别名称的方式相同:
@foreach($categories as $category)
<option name="pid" value="{{$category->id}}" id="cname">{{$category->name}}</option>
@endforeach
您从 Laravel 中的数据库调用的所有内容都应具有 {{$category->someName}}
格式,无论您将其放置在视图中的什么位置。在您的情况下,您需要将 $category->id
括在 {{ }}
中
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选择的名称也存储在数据库中。
像这样:
@foreach($categories as $category)
<option name="pid" value="$category->id" id="cname">{{$category->name}}</option>
@endforeach
这些值从我的控制器传递到 Blade 模板。如您所见,我希望它在下拉列表中显示类别名称(这很好),但是一旦选择并提交,我希望返回类别 ID。由于 "value" 属性 returns 的值,我想将变量 $category->id
作为 "value"。我想在不使用 JavaScript 或 jQuery 的情况下执行此操作。只有 PHP。我将如何着手完成这个,甚至可能吗?
当我尝试使用 value="{{$category->id}}"
时,出现 Laravel 错误:
Integrity constraint violation: 1048 Column 'parent_id' cannot be null
所以它似乎没有提交下拉列表的值。
这是我目前在我的 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');
}
与在选项标签内显示类别名称的方式相同:
@foreach($categories as $category)
<option name="pid" value="{{$category->id}}" id="cname">{{$category->name}}</option>
@endforeach
您从 Laravel 中的数据库调用的所有内容都应具有 {{$category->someName}}
格式,无论您将其放置在视图中的什么位置。在您的情况下,您需要将 $category->id
括在 {{ }}