Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Seif5's avatar
Level 3

Ajax request repeat the same value

I really searched for information more than a week, but I did not find solution. I work with ajax in laravel, my target is get value from post type attachment with ajax . the problem is ajax load only the first value wish I have a foreach loaded all my data on the model

This work but repeat the same value I have 6 question, but the request just repeat the first , not continue with 2, 3,4 5,6..etc. I want to be better, if you find something wrong with my code, please tell me how to improve. Thank you very much.

Question.blade.php


<?php $i = 0; ?>

@foreach ($questions as $question)

<h2>Welcome to The Accident Guys</h2>


<section data-step="<?php echo $i  ?>" >
    <div class="col-sm-12" id="headersection" >
        <h4><b>Q.1</b></h4>
    </div>

    <div class="col-md-12 col-lg-12" style="padding-top:50px;">
    
    <div style="background:gold">
        <ul>
            <li name="idquestion2" id="idquestion2" value="{{ $question->id }}">{{ $question->id }}
            </li>
            <li>{{ $question->content }}</li>
            <li>{{ $question->a1 }}</li>
            <li>{{ $question->a2 }}</li>
            <li>{{ $question->a3 }}</li>
            <li>{{ $question->a4 }}</li>
            <li>{{ $question->a5 }}</li>
            <li>{{ $question->a6 }}</li>
            <li>{{ $question->a7 }}</li>
            <li>{{ $question->a8 }}</li>
        </ul>
        <br>
    </div>
   
    </div>

</section>
<?php  $i++ ?>

 @endforeach

script.js

        //after function..
           e.preventDefault();
            $.ajax({
            type: "GET",
            url: '/setQuestion',
            data: {idquestion2:$('#idquestion2').val()},

            cache: false,
            success: function(data){
               console.log(idquestion2);

            }
            });
                              var idquestion2 = $("#idquestion2").val();

mycontroller.php

public function setQuestion(Request1 $request){
        $data = Input::all();
    $idquestion2 =  Request1::input('idquestion2');

if(Request2::ajax())
        {

    $user = Auth::user();
    $history = new History();
    $user_id = Auth::user()->id;
    $history->user_id = $user_id;
      $history->question_id = $idquestion2;

    $history->user_resulat = 'Hell22o';

  $history->save();
        }
        else{
            dd('I am not ajax request');
        }




}
0 likes
5 replies
Seif5's avatar
Level 3

any one can help me please!

kingpabel's avatar

Please look you are populating id "idquestion2' in foreach,that means same id will populate multiple time,that is not right.That's why you are facing this.

Seif5's avatar
Level 3

@kingpabel thankyou for your answer , I trought that I should browe all questions ? you have an idea how i can fix that?!

kingpabel's avatar

@Amelie89 In my suggestion there has 2 options to solve.Easiest solution is generate a dynamic id like idquestion{{ $question->id }} and use your javascript inside foreach loop,like

 e.preventDefault();
            $.ajax({
            type: "GET",
            url: '/setQuestion',
            data: {idquestion:$('#idquestion{{ $question->id }}').val()},

            cache: false,
            success: function(data){
               console.log(idquestion);

            }
            });
                              var idquestion = $("#idquestion{{ $question->id }}").val();

Another solution is work using class not id,and then catch the current class when click or any other event using this operator.

Please or to participate in this conversation.