@seaniz if you've got access to the data in php why not do the 'totaling up' before you pass it to the blade?
Using Jquery to total input values of all questions with a specific "tag" in a foreach loop.
My title's a mouthful but that's the question, I don't know how to word in any other way haha. Okay so I have a loop of questions that is from my model Survey.php.
This is my loop:
<section id="choices_array" class="class">
@foreach($class as $question)
<div class="question border-gray-400 pt-8 pb-2 mx-auto w-3/5 inactive {{$loop->first ? '':'border-t'}} {{$loop->last ? '':'border-b'}}">
<div class="flex justify-center text-2hxl text-gray-700 font-medium pb-6">
<span class="text-center w-3/5">{{ $question->question }}</span>
</div>
<div class="pb-6 choices">
@include('survey.partials.mediumbuttons')
</div>
<input type="hidden" name="responses[{{ $question->id }}][question_id]" value="{{ $question->id }}">
<input type="hidden" name="responses[{{ $question->id }}][question_tag]" value="{{ $question->tag }}">
<input type="hidden" name="responses[{{ $question->id }}][question_category]" value="{{ $question->category }}">
</div>
@endforeach
</section>
This is controller for the view you see above, to understand what $class is.
public function FirstPage(Request $request, Survey $survey)
{
$class = $survey->questions()->where('category', 'Class');
return view('questionnaire.firstpage', compact('survey', 'class'));
}
I wanna use Jquery to total up the values from my radio buttons for each question. My radio buttons is @include('survey.partials.mediumbuttons').
But I want Jquery to total up values for specific questions, like questions with a "question_tag" of "food". And another one for "drinks". And then equal those values into each respective variables.
Then using those variables, I would like to do if and else statements which will reveal specific depending on which variable that has the higher value.
My question is, is it possible to total up questions with specific tags...? I know that I can pass eloquent variables into javascript but because I'm not saving any data into my database yet, hence I can't really use "data" from my models to carry out the conditional statements.
Sorry if the question has an obvious answer which I can google myself, I'll recheck again while waiting for a response. Thanks in advance! :)
@seaniz ah. that makes sense.
Sounds like quite a complicated bit of jquery here, especially as you storing the data in multiple hidden inputs.
would you be able to make selecting the question_tag hidden fields a bit easier by giving them a class to help select them?
eg
<input type="hidden" class='question_tag' name="responses[{{ $question->id }}][question_tag]" value="{{ $question->tag }}">
and then you can add them by
let food = 0;
$('.question_tag').each(function (index) {
if ($(this).value === 'food') {
food++; // or whatever your corresponding field that holds answer value
}
});
Please or to participate in this conversation.