likwidmonster
likwidmonster

Reputation: 413

Laravel Request show what form the input was captured in

I am currently using the Laravel 5.4 Framework and during form submission I am collecting the inputs of a Request and would like to see the form name that the inputs were captured in.

<form name="my_form">
    <input type="checkbox" name="my_input" value="my_value">
</form>

So in the example above I would like to see the checkbox named "my_input" to be contained in the form "my_form". Currently with the way Request works it will just take "my_input" and show that.

The reason for my need of seeing the form name because my inputs are dynamic and am creating objects based off of the form name.

Thank You

Upvotes: 0

Views: 774

Answers (1)

Moshe Katz
Moshe Katz

Reputation: 16891

When you create a <form> with some name, also create a field in the form with that name as its value.

For example, using the form in your question:

<form name="my_form">
    <input type="hidden" name="form_name" value="my_form">

    <input type="checkbox" name="my_input" value="my_value">
</form>

Then you can use $request->input('form_name') in Laravel (or any of the other ways to get input data).

If you dynamically change the name of the form, you can use the same code to change the value of the field. For example (with jQuery):

function setFormName(oldName, newName) {
    var form = $('form[name="' + oldName + '"]');
    form.attr('name', newName);
    $('input[name="form_name"]', form).val(newName);
}

This can also be done without jQuery, but it's a bit more code, so it is left as an exercise to the reader.

Upvotes: 1

Related Questions