Php 如何发送单选按钮值而不在laravel中提交?
我想发送单选按钮值而不提交 这就是观点 这是查看代码Php 如何发送单选按钮值而不在laravel中提交?,php,jquery,ajax,laravel,Php,Jquery,Ajax,Laravel,我想发送单选按钮值而不提交 这就是观点 这是查看代码 <table> <thead> <tr> <th>No.</th> <th>Name</th> <th><center>Presence</center></th>
<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>
在上面的代码中,如果单击“状态”按钮,即使我在控制台浏览器中签入,也不会发生任何事情
如果我按下状态按钮Yes或No,它会将值发送到数据库 在输入中添加csrf的隐藏令牌 像这样
<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代码没有
CSRF
令牌,请检查您的浏览器控制台
@Ivan我刚才在Ajax代码中添加了CSRF令牌,所以我添加了“_令牌”:“{{CSRF_token()}”。但是仍然不走运。请检查浏览器控制台并在此发布错误Get 500 internal server error如果您的控制台返回500 internal erorr,您应该检查您的Laravel代码,检查登录project\storage\logs
<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({
url:"/admin/presence/add",
method:"POST",
data:{
'_token': $('input[name=_token]').val(),
'presence':presence
},
success:function(data){
}
});