如何从Laravel 5.2中的textarea获得价值?

时间:2016-11-11 17:22:43

标签: php laravel laravel-5 laravel-5.2

我目前正在使用Laravel 5.2,我发现难以从控制器内的textarea获取值。

我尝试$request->input就像我们以前从输入中获取值一样,但它在我的情况下不起作用。我做到了这一点。

managecategories.blade.php

<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>

AdminAjaxController

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);
}

1 个答案:

答案 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">

谢谢你的伴侣! :)