php - 如何在 Laravel 中发送单选按钮值而不提交?

标签 php jquery ajax laravel

我想发送单选按钮值而不提交。

这是 View

enter image description here

这是查看代码

<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>  

这是 Controller

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

使用上面的代码,如果我单击状态按钮,即使我在控制台浏览器中 checkin ,也不会发生任何事情。

我想如果我按"is"或“否”按钮,它会将值发送到数据库。

最佳答案

在您的输入中添加 csrf 的隐藏 token 。

像这样

<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>

然后在你的ajax中删除localhost,路径就可以了,然后添加csrf

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

  }  
});  

不知道radio有没有点击事件你也可以试试这个one如果单击单选按钮,则可以选择其他选项。

希望对你有帮助!

关于php - 如何在 Laravel 中发送单选按钮值而不提交?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56944368/

相关文章:

javascript - 使用 jquery 或 javascript 从对象获取变量

jquery - 如何删除 :hover on phones after <a> is clicked

java - Ajax - API 端点 - 这是正确的方法吗?

php - 使用 preg_replace 返回引用数组键并替换为值

php - Laravel 5 验证唯一(数据库)仅适用于相同的外键

php - mysql 到 mysqli 类函数转换

javascript - 如何判断 HTML 下拉列表是否显示选项列表

javascript - jQuery.post 引发 INVALID_STATE_ERR (11)

ajax - 在 Yesod 中允许跨域请求

php - webapp被黑了,但是插入的确切代码是什么? echo (gzinflate(base64_解码(".....")