drsl90685154 2017-11-25 11:53
浏览 144

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

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

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

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
}
  • 写回答

1条回答 默认 最新

  • dongshi1148 2017-11-25 12:08
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题