简体   繁体   English

LARAVEL:如何将数据从视图发送到控制器

[英]LARAVEL: how to send data from view to controller

I need to send a value of <input type='CHIPS'> 我需要发送<input type='CHIPS'>

from add.blade.php to userController@add.php add.blade.phpuserController@add.php

this is the code of add.blade.php file: 这是add.blade.php文件的代码:

<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 这是add.blade.php文件scripte:我来自哪里chips_form得到的数据和我将它们设置在一个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.php文件

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

userController.php file : userController.php文件:

    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
}

To send the contenct of dataString to the controller, you can create a hidden input in the form, and set its value using jQuery. 要将dataString的内容发送给控制器,您可以在表单中创建一个隐藏的输入,并使用jQuery设置其值。

First, add an input to the form: 首先,向表单添加输入:

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

Then, set its value using jQuery: 然后,使用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. 当用户发送表单时,该值将在隐藏的输入中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 ajax 从视图发送数据到 controller laravel? - How to send data from view to controller laravel with ajax? 如何将数据从 Laravel 控制器发送到 vuejs - How to send data from laravel controller to vuejs 如何在Codeigniter中将数据从视图发送到控制器 - How to send data from view to controller in codeigniter 如何在没有Ajax的情况下将数据从View发送到Controller? - How to send data from View to Controller without ajax? 如何将AJAX数据从VIEW发送到CONTROLLER? (PHP(MVC)+ AJAX) - How to send AJAX data from VIEW to CONTROLLER? (PHP(MVC)+AJAX) 我如何从我的控制器发送 json 数据来查看? - how can i send json data from my controller to view? 如何在Spring Boot中将数据从视图发送到控制器 - How to send data from view to controller in spring boot 如何将全局变量数据从视图发送到控制器? - How can I send Global varibale data from view to controller? 如何将WebSocket数据从View发送到Controller,以及如何将经过重做的数据通过Ajax发送到View - How to send WebSocket data from View to Controller and reworked data send to View with Ajax 在 Laravel 中,如何简单地将数据从 js 传递到 controller 返回查看? - In Laravel, how to simply pass data from js to controller back to view?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM