php - Laravel redirect with javascript
I have a form where a user can select a number ranging from 2 to 10. Based on the selected number I'd like to redirect to'/mypage?number=[2..10]'
like the way Laravel's redirect() function does. Thus, flashing the old input values to fill the form accordingly. Based on the $_GET['number'] parameter a for-loop is executed printing 2 to 10 input fields.
How can this be done in the most efficient (and meant to be) way? Or do I have to set up a new route likeRoute::post('mypage/{number}', '[email protected]')
?
Answer
Solution:
You are probably over-thinking this. If this is in a form, just have the form action be
/mypage
and set the name of the select tonumber
and the form's method toget
. When the form is submitted, it will bring the user to the page. If you'd like it to auto-submit when the user modifies the dropdown, you can submit the form programatically via js/jquery$('#myForm').submit()
.There should be no need for additional routes just to handle the
number
parameter. Use\Input::get('number')
to grab the value on the server side.Answer
Solution:
How will the javascript get
{number}
all you need to do in the javascript
location.href
Answer
Solution:
1) Either redirect should take place on the server - when you send params on sever and it brings you the right page or redirects you to the right page.
2) Or you need to generate javascript in laravel_blade and set this logic in the Javascript. Say, generate input fields with extra data- attribute and make javascript to handle this attribute.
3) Or maybe, avoid redirects at all and make javascript generate variable amount of input fields on the same page judging on data-attributes of the button that was pressed.