aladin
aladin

Reputation: 1

LARAVEL: how to send data from view to controller

I need to send a value of <input type='CHIPS'>

from add.blade.php to [email protected]

this is the code of add.blade.php file:

<div class="container">
        <form method="POST" action="{!! url('add') !!}" id="ajouter" accept-charset="UTF-8">

            <label for="nom">Entrez votre nom : </label>
            <input name="name" type="text"  id="name">

            <input name="email" type="email" id="email">

            <input name="password" type="password" id="password">

            <label for="permution">saisier les parmutions : </label>

            <div class="chips" name="permution"></div>

            <input class="btn-info" type="submit" value="Envoyer !">

            <div id="permution" name="permution">

            </div>

        </form>
    </div>

and this is the scripte in add.blade.php file : where i got data from chips_form and I set them in a new value of type String

<!--Import jQuery before materialize.js-->
    <script type="text/javascript" src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
    <script type="text/javascript" src="css/materialize/js/materialize.min.js"></script>


    <script>
        $(function() {

            var data = $('.chips').material_chip();
            var dataString = JSON.stringify(data);

        });
    </script>

route.php file:

Route::get('/add', 'UsersController@add_form');
Route::post('/add', 'UsersController@add');

userController.php file :

    public function add_form()
        {
            return view('add');
        }


        public function add(Request $request)
        {
            $name = $request->input('name');
            $email=$request->input('email');
            $password = $request->input('password');
            $cryptPassword= bcrypt($password);

            //Insert SQL Request
}

Upvotes: 0

Views: 895

Answers (1)

Laerte
Laerte

Reputation: 7073

To send the contenct of dataString to the controller, you can create a hidden input in the form, and set its value using jQuery.

First, add an input to the form:

<input id="ichips" type="hidden" name="chips">

Then, set its value using jQuery:

var data = $('.chips').material_chip();
var dataString = JSON.stringify(data);
$("#ichips").val(dataString);

When the user send the form, the value will be in the hidden input.

Upvotes: 1

Related Questions