php - Laravel redirect with javascript

262

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]')?

913

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 thenumber parameter. Use\Input::get('number') to grab the value on the server side.

74

Answer

Solution:

How will the javascript get{number}

all you need to do in the javascriptlocation.href

831

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.

People are also looking for solutions to the problem: php - Calculate totals for each column of a two dimensional array (matrix)

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.