Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to add a value to a submit button that gets passed in the params?

I'm trying to add a "preview" button/page to my app.

Is there a way to add a different value to both my "post" and "preview" submit buttons that get passed in the params hash so that I can check which one was pressed in the controller and render the view accordingly?

Is this the best way to do this?

like image 258
Jon Avatar asked Jun 14 '11 18:06

Jon


People also ask

Can a submit button have a value?

Value. An <input type="submit"> element's value attribute contains a string which is displayed as the button's label. Buttons do not have a true value otherwise.

How do you pass a value on submit?

You have to pass the parameter in the request. Since you are having a form, and submitting the form, you can have a hidden field in the form called, say "submitType", and populate it whenever you click the button, using javascript. Then this will be available in the next request.

How can get submit button value in PHP?

Add type property with submit type="submit" to button tag this will submit the form then receive the form inputs value in php script with super global variable $_POST['input name'] or $_GET[] wdepends on from action property value by default GET.

What happens when submit button is clicked?

The form will be submitted to the server and the browser will redirect away to the current address of the browser and append as query string parameters the values of the input fields.


1 Answers

The keys of the params hash are just the name value of an element.

If you had two buttons named "submit", one with value="post" and value="preview", then you could do something like:

if params[:submit] == "preview"
like image 98
danneu Avatar answered Oct 19 '22 07:10

danneu