[英]The data in the loop and ajax data are different
foreach 循環中有單選按鈕和一些隱藏字段,但我在循環中出錯,無法獲得正確的數據。 因此,作為 ajax 錯誤:SQLSTATE [23000]:完整性約束違規:1452 無法添加或更新子行:外鍵約束失敗錯誤。
Json 文件如下
{"data":[
{
"id":1,
"question":"Test Question 1",
"description":"Test Poll Desription",
"creator_id":1,
"results_count":2,
"message_type":"poll",
"poll_result":3,
"creator":{
"id":1,
"name":"Test",
"email":"test@hotmail.com",
"created_at":"2019-05-28T23:29:59.000000Z",
"updated_at":"2020-02-28T18:37:20.000000Z"
},
"option":[
{
"id":1,
"poll_id":1,
"option":"Test 1 Title",
"poll_results_count":0,
"poll_results":[
]
},
{
"id":2,
"poll_id":1,
"option":"Test 2 Title",
"poll_results_count":0,
"poll_results":[
]
},
{
"id":3,
"poll_id":1,
"option":"Test 3 Title",
"poll_results_count":2,
"poll_results":[
{
"id":1,
"poll_id":1,
"poll_option_id":3,
"user_id":4
},
{
"id":34,
"poll_id":1,
"poll_option_id":3,
"user_id":9
}
]
}
]
}
]
}
查看文件
@foreach ($data['option'] as $key_option => $option)
<label class="checkerCircle">
{{ isset($option['option']) ? $option['option'] : 'Default' }}
<div class="progress-container">
<div class="progress">
<div class="progress-5"></div>
</div>
<span>
@if ($option['poll_results_count'] == 0)
0%
@else
@php
$yuzde = (100/$sum)*$option['poll_results_count'];
echo $yuzde."%";
@endphp
@endif
</span>
</div>
<input type="hidden" name="poll_id" value="{{ $option['poll_id'] }}">
<input type="radio" name="option_id" value="{{ $option['id'] }}">
<input type="hidden" name="user_id" value="{{Auth::user()->id}}">
<span class="checkerCircle__span"></span>
</label>
@endforeach
在上面的代碼中,我添加了輸入單選並將輸入隱藏到循環中。
在同一個視圖文件中,ajax如下。
<script type="text/javascript">
$(document).ready(function(){
$('input[type="radio"]').click(function(){
var option_id = $(this).val();
var poll_id = $(this).val();
var user_id = $(this).val();
$.ajax({
url: "{{ route('pool.ajax.home')}}",
type: "POST",
dataType: 'json',
data:{
"_token": "{{ csrf_token() }}",
option_id:option_id,
poll_id:poll_id,
user_id:user_id,
},
success:function(response){
console.log(response);
},
error:function(e){
console.log(JSON.stringify(e));
console.log('error');
}
});
return false;
});
});
</script>
在這些過程之后,只有第一個輸入有效。 當我單擊第二個或第三個單選按鈕時,它會給出 sql 事務錯誤,因為它發送了錯誤的數據。
例如,當我查看頁面源代碼時,第二個單選按鈕的值在 HTML 字段中看起來像這樣。
<input type="hidden" name="poll_id" value="1">
<input type="radio" name="option_id" value="2">
<input type="hidden" name="user_id" value="9">
但是ajax自帶的數據如下
option_id=2
poll_id=2
user_id=2
我在某處做錯了什么,我不知道是什么
此致
好的問題已解決循環文件和 ajax 變量中有錯誤。
查看刀片文件
<div class="l-flex">
@php
$sum = array_sum(array_column($data['option'],'poll_results_count'));
@endphp
@foreach ($data['option'] as $key_option => $option)
<label class="checkerCircle">
{{ isset($option['option']) ? $option['option'] : 'Default' }}
<div class="progress-container">
<div class="progress">
<div class="progress-5"></div>
</div>
<span>
@if ($option['poll_results_count'] == 0)
0%
@else
@php
$yuzde = (100/$sum)*$option['poll_results_count'];
echo $yuzde."%";
@endphp
@endif
</span>
</div>
<input type="hidden" id="{{ $option['id'] }}" name="poll_id" value="{{ $option['poll_id'] }}">
<input type="radio" name="option_id" value="{{ $option['id'] }}" >
<span class="checkerCircle__span"></span>
</label>
@endforeach
<input type="hidden" name="user_id" value="{{Auth::user()->id}}">
</div>
以下代碼屬於ajax
<script type="text/javascript">
$(document).ready(function(){
$('input[type="radio"]').click(function(){
var option_id = $(this).val();
var poll_id = $('input#'+option_id+'').val();
var user_id = $('input[name="user_id"]').val();
$.ajax({
url: "{{ route('pool.ajax.home')}}",
type: "POST",
dataType: 'json',
data:{
"_token": "{{ csrf_token() }}",
option_id:option_id,
poll_id:poll_id,
user_id:user_id,
},
success:function(response){
console.log(response);
location.reload(true);
},
error:function(e){
console.log(JSON.stringify(e));
console.log('error');
}
});
return false;
});
});
</script>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.