我目前正在使用Laravel 5.2,我发现难以从控制器内的textarea
获取值。
我尝试$request->input
就像我们以前从输入中获取值一样,但它在我的情况下不起作用。我做到了这一点。
<form action="/addcategories" method="post" id="addcategory_form" name="addcategory_form">
<div>
<label>Insert Category</label>
<input type="text" name="category" data-validation="required" data-validation="custom" data-validation-regexp="^[a-zA-Z ]{2,30}$">
</div>
<div>
<label>Insert Category</label>
<textarea name="category" data-validation="required" data-validation="custom" data-validation-regexp="^[a-zA-Z ]{2,30}$" row="5" col="200"></textarea>
</div>
<input type="submit" value="Add Category" id="category_btn">
</form>
public function addcategory(Request $request)
{
$category=$request->input('category');
$category_description=$request->input('description');
$insertcategory= DB::insert('insert into categories(category_name,description) values(?, ?)',[$category,$category_description]);
$fetch_category= DB::select('select category_name,category_id from categories');
return response()->json(array('add_category' => $category),200);
}
答案 0 :(得分:0)
糟糕可惜我!这两个字段都命名为category。我需要将textarea上的名称更改为Aynber捕获的描述。
这是经过编辑的正确代码
<form action="/addcategories" method="post" id="addcategory_form" name="addcategory_form">
<div>
<label>Insert Category</label>
<input type="text" name="category" data-validation="required" data-validation="custom" data-validation-regexp="^[a-zA-Z ]{2,30}$">
</div>
<div>
<label>Insert Category</label>
<textarea name="description" data-validation="required" data-validation="custom" data-validation-regexp="^[a-zA-Z ]{2,30}$" row="5" col="200"></textarea>
</div>
<input type="submit" value="Add Category" id="category_btn">
谢谢你的伴侣! :)