最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to send parameter to route on form submit - Stack Overflow

programmeradmin1浏览0评论

I have the following route:

Route::get("/changeLang/{lang}", 'SiteController@changeLang');

And I have the following form:

<form action="{{ url('/changeLang/???') }}" method="GET">
    {{ csrf_field() }}
    <select onchange="this.form.submit()" name="lang">
        <option value="eng">English</option>
        <option value="cro">Hrvatski</option>
    </select>
</form>

So I'd like to submit that form to that route so that the route receives the language that the user selected. I don't know should form method be GET or POST.

So how do I specify link to that route in form action so that the selected language gets sent? What to put instead of those question marks at the end of the URL?

And I don't like to use Laravel's Form facade (or is it just class) to echo HTML elements, so I'd like to do it without that if possible.

I have the following route:

Route::get("/changeLang/{lang}", 'SiteController@changeLang');

And I have the following form:

<form action="{{ url('/changeLang/???') }}" method="GET">
    {{ csrf_field() }}
    <select onchange="this.form.submit()" name="lang">
        <option value="eng">English</option>
        <option value="cro">Hrvatski</option>
    </select>
</form>

So I'd like to submit that form to that route so that the route receives the language that the user selected. I don't know should form method be GET or POST.

So how do I specify link to that route in form action so that the selected language gets sent? What to put instead of those question marks at the end of the URL?

And I don't like to use Laravel's Form facade (or is it just class) to echo HTML elements, so I'd like to do it without that if possible.

Share Improve this question edited Feb 2, 2017 at 13:34 Scarass asked Feb 2, 2017 at 13:26 ScarassScarass 9542 gold badges14 silver badges32 bronze badges 1
  • well form submission is not going to work since it would be /changeLang/lang?lang=cro – epascarello Commented Feb 2, 2017 at 13:32
Add a ment  | 

4 Answers 4

Reset to default 3

You can not submit a form and have it generate a URL in the format that you are expecting. What is going to happen is you will get a URL in the format of /changeLang/lang?lang=cro

So you have two options. You submit the form and it redirects based off the querysting value.

OR you add JavaScript to your form so you produce the url that you are after.

<select onchange="window.location.href='/changeLang/' + this.value;" name="lang">

if your route is something like this.

Route::get("/changeLang", 'SiteController@changeLang');

your form will be something like this.

<form action="{{ url('/changeLang') }}" method="GET">
{{ csrf_field() }}
<select onchange="this.form.submit()" name="lang">
    <option value="eng">English</option>
    <option value="cro">Hrvatski</option>
</select>

to get value from GET[POST optionally after changing route], in your controller do something like this.

public function changeLang(Request $request)
{
    $data = $request->all();
    //echo $request->lang;
    // or you can access data like this.
    echo $data['lang'];
}

and if you want this route

Route::get("/changeLang/{lang}", 'SiteController@changeLang');

then you need to append this to the route and request url will be something like this.

https://host/changeLang/eng

and in your controller.

public function changeLang($lang)
{
    echo $lang;
}

url(route)

The url function generates a fully qualified URL to the given path

more about url.

Hope this will help!.

Use this

<form action="{{ url('/changeLang', [$lang]) }}" method="GET">
    {{ csrf_field() }}
    <select onchange="this.form.submit()" name="lang">
        <option value="eng">English</option>
        <option value="cro">Hrvatski</option>
    </select>
</form>

Or this

<form action="{{ url('/changeLang/' . $lang,) }}" method="GET">
    {{ csrf_field() }}
    <select onchange="this.form.submit()" name="lang">
        <option value="eng">English</option>
        <option value="cro">Hrvatski</option>
    </select>
</form>

Better use laravel form builder:

{{ Form::open([
      'method' => 'GET',
      'action' => ['some@action', $param]
]) }}

...

{{ Form::close() }}

Or:

 <form action="{{ url('/changeLang/lang' . $param) }}" method="GET">
发布评论

评论列表(0)

  1. 暂无评论