doumindang2416 2019-07-09 01:49
浏览 109
已采纳

如何发送单选按钮值而无需在laravel中提交?

I want to send the radio button value without submit.

This is the view

enter image description here

This is view code

<table>
        <thead>
            <tr>
                <th>No.</th>
                <th>Name</th>
                <th><center>Presence</center></th>
            </tr>
        </thead>
        <tbody>
                @php
                 $i=1;
                @endphp
            <tr>
                <td>{{$i}}</td>
                <td>TEST</td>
                <td>

                    <input type="radio" id="radio1" name="presence" value="Y">
                    <label for="radio1"><font color="#00B01D"><i class="fas fa-check"></i> Yes</font></label>
                    <input type="radio" id="radio2" name="presence" value="N">
                    <label for="radio2"><font color="#FF0000"><i class="fas fa-times"></i> No</font></label>
                </td>

            </tr>
            @php
                $i++;
            @endphp
        </tbody>
    </table>

<script>  
            $(document).ready(function(){  
                    $('input[type="radio"]').click(function(){  
                        var presence = $(this).val();  
                        $.ajax({  
                            url:"http://localhost/admin/presence/add",  
                            method:"POST",  
                            data:{
                                  presence:presence
                                  },  
                            success:function(data){  

                            }  
                        });  
                    });  
            });  
    </script>  

And this is controller

public function addprespost(Request $request){
        $dataz = [
            'presence' => $request->input('presence'),
        ];
        DB::table('presence')->insert($dataz);
        return redirect('/admin/presence/add');
    }

With my code above, if I click the presence button, there is nothing happen, even I checked in console browser.

I want to make if I press the presence button Yes or No, it'll send the value to the database.

  • 写回答

1条回答 默认 最新

  • dsmgcse8876 2019-07-09 02:20
    关注

    Add hidden token for csrf in your input.

    like this

    <input type="hidden" name="_token" value="{{ csrf_token() }}">
    <input type="radio" id="radio1" name="presence" value="Y">
    <label for="radio1"><font color="#00B01D"><i class="fas fa-check"></i> Yes</font></label>
    <input type="radio" id="radio2" name="presence" value="N">
    <label for="radio2"><font color="#FF0000"><i class="fas fa-times"></i> No</font></label>
    

    then in your ajax remove the localhost just the route is fine and add the csrf

    $.ajax({  
      url:"/admin/presence/add",  
      method:"POST",  
      data:{
       '_token': $('input[name=_token]').val(),
       'presence':presence
      },  
      success:function(data){  
    
      }  
    });  
    

    I don't know if radio have a click event you can also try this one for other option if radio is clicked.

    Hope it helps!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料