laravel 如何在blade.php中获取输入值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48263263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:14:45  来源:igfitidea点击:

How to get value of input in blade.php

phplaravellaravel-5

提问by xamarinDev

I need to get a value of input to use below, how to do that? I tried to like this but error says

我需要获得一个输入值以在下面使用,该怎么做?我试图喜欢这个但错误说

Undefined variable: name

未定义变量:名称

<div class="col-md-10 col-md-offset-1">
      <input id="name" type="text" name="name" />
</div>


<div class="col-md-10 col-md-offset-1">
    @php
       $nameValue=$_GET['name'];
    @endphp
    <input id="name2" type="text" name="name2" value="{{$nameValue}}" />
</div>

回答by Brotzka

You have to be aware that your input-values (here "name") ist only available after submitting the form.

您必须注意,您的输入值(此处为“名称”)仅在提交表单后才可用。

If you want to access the form-values before submitting you should take a look at VueJS or any other frontend-framework (React, Angular). Or simply use jQuery.

如果您想在提交之前访问表单值,您应该查看 VueJS 或任何其他前端框架(React、Angular)。或者干脆使用jQuery。

Therefor you have to use JavaScript if you want to use the input-value before submitting.

因此,如果您想在提交前使用输入值,则必须使用 JavaScript。

Like the others said in the comments, you can access your form-values within your controller and then pass it to your view.

就像评论中的其他人所说的那样,您可以在控制器中访问表单值,然后将其传递给您的视图。

For example (from the documentation):

例如(来自文档):

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UserController extends Controller
{

    public function formSubmit(Request $request)
    {
        $name = $request->input('name');

        return view('form', ['name' => $name])
    }
}

Now you can use the value within your view:

现在您可以在视图中使用该值:

<input id="name2" type="text" name="name2" value="{{$name}}">

Another possibility would be to "by-pass" your controller and return your view directly from your routes.php:

另一种可能性是“绕过”您的控制器并直接从您的 routes.php 返回您的视图:

Route::get('/form-submit', function(){
    return view('form');
});

But I'm not sure if this is working and you could access $_GET/$_PSOT directly without using Laravels Request.

但我不确定这是否有效,您可以直接访问 $_GET/$_PSOT 而不使用 Laravel 请求。

回答by Luca C.

$nameValue=Request::input('name')

From the blade template you can access the request parameters with the Requestfacade, you can also print it directly:

从刀片模板中,您可以访问带有Request门面的请求参数,也可以直接打印它:

{{Request::input('name')}}

In latest versions you can also use:

在最新版本中,您还可以使用:

{{request()->input('name')}}

回答by Mahdi Khansari

You can get inputs array from Request class:

您可以从 Request 类获取输入数组:

Request::all()['your_input']

Also you can check if that input you want is exists not:

您也可以检查您想要的输入是否存在:

@isset(Request::all()['your_input'])
     {{-- your input existed --}}
@else
     {{-- your input does not existed --}}
@endisset