1

I'm trying to create a form that passes data via get to the controller but the URL looks allways like this:

http://example.com/test?_token=VinwWFxKIhKvMqrrEBN5xwXhrmYQjLnOWV8s7dht&param1=horse&param2=cat&param3=dog

But I want something like this:

http://example.com/test/param1=horse/param2=cat/param3=dog

or

http://example.com/test/horse/cat/dog

Route:

Route::get('test/{param1}/{param2}/{param3}', ['as' => 'test', 'uses' => 'MainController@test']);

HTML:

<form action="{{ route('test') }}" method="get">
            {{ csrf_field() }}
            <div class="col-md-3">
                <div class="form-group">
                    <label for="animal1">animal1</label>
                    <br>
                    <input type="text" name="animal1" class="form-control">
                </div>
            </div>
            <div class="col-md-3">
                <div class="form-group">
                    <label for="animal2">animal2</label>
                    <input type="text" name="animal2" class="form-control">
                </div>
            </div>
            <div class="col-md-3">
                <div class="form-group">
                    <label for="animal3>animal3</label>
                    <input type="text" name="animal3" class="form-control">
                </div>
            </div>
            <div class="col-md-12">
                <button type="submit" class="btn btn-default">Submit</button>
            </div>
</form>

The problem is that the test route is reloaded every 10 seconds. Therefore, the form values must be in the URL so that I can process them correctly in the controller.

I've found this question here but that wasn't so helpful

How To Pass GET Parameters To Laravel From With GET Method ?

Thanks for your help!

dwdawdawdaw
  • 251
  • 1
  • 6
  • 20

2 Answers2

0

To do this, you'll need to switch off CSRF token check, which is a bad idea. Or you could use JS to build the query which is not a good idea too.

The best way to handle this is to use POST instead of GET:

<form action="{{ route('test') }}" method="post">

And then change the route to:

Route::post('test', ['as' => 'test', 'uses' => 'MainController@test']);
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
  • But if I do a post request, the page can't get reloaded every 10 automatically. After the 10 secconds I get a " MethodNotAllowedHttpException" – dwdawdawdaw Jan 14 '18 at 10:40
  • I think the error comes because the data expected by the function will not be given if the page is reloaded automatically. – dwdawdawdaw Jan 14 '18 at 10:43
  • @dwdawdawdaw please describe this every 10 seconds refresh thing. – Alexey Mezenin Jan 14 '18 at 10:46
  • The view called after the test function is reloaded every 10 seconds with this line in the head section: The "animal" data I send to the controller before are actually settings that tell an API which data I want to have exactly. – dwdawdawdaw Jan 14 '18 at 10:50
  • @dwdawdawdaw use AJAX instead of this if you want to send some data every 10 seconds to the back-end. – Alexey Mezenin Jan 14 '18 at 10:53
  • mhhh.. I think I will do that. Then I need to tell ajax the settings I told him before and render the api data to the old table by replacing them with the new data – dwdawdawdaw Jan 14 '18 at 10:56
0

You can do this via javascript. You don't need to use form like this.you can just get input values by id(getElementById) and on a button click, format them as you expect (test/{param1}/{param2}/{param3}) and redirect page to that.

M.ah
  • 147
  • 11