How to get value of input in blade.php

邮差的信 提交于 2021-02-10 06:32:29

问题


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>

回答1:


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.

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

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:

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.




回答2:


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

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

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

In latest versions you can also use:

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



回答3:


You can get inputs array from Request class:

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


来源:https://stackoverflow.com/questions/48263263/how-to-get-value-of-input-in-blade-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!