How to send parameters from my blade view to a Controller - Laravel 5.6

0

I need to send a variable in javascript that is in my view in Blade and send it to a controller that it would save in the database. Can you give me an example?

    
asked by franmavazq 10.10.2018 в 17:41
source

1 answer

1

Several situations can occur:

1) The variable is part of a form If this is the case and if you want the variable to be sent when submitting, you can add a hidden input and set the value of the variable with Javascript, like this:

In HTML:

<form>
...
<input type="hidden" name="nombreQueSeEnviaraAlController" id="idEnHTML" value="">

In Javascript, after calculating / obtaining the variable:

inputDeVariable = document.getElementById("idEnHTML");
inputDeVariable.value = variable

2) The variable is independent of the form

Use AJAX. I do not know if you are familiar with AJAX. AJAX allows you to send requests and receive answers without having to make a complete request of the entire HTML form. Personally I would say that they are "partial" forms that are sent with Javascript and likewise you work with the answer. Here is a more formal definition ( link )

You prepare your controller to work with Ajax: Requests ajax in Laravel

And from Javascript send the variable ( link ) or you can use jQuery also ( link ).

Using AJAX the Controller receives the request as if it were a classic HTML form. To read the parameters you must use the Request object of Laravel.

    
answered by 10.10.2018 / 18:43
source