0

I'm beginner in Ajax. I'm getting 'Undefined variable $doctor' If I have not any record in database I got this error but It works when I delete the line starting with 'prepend' in Ajax script. Where is my problem?

My Controller is:

public function doctors(){
        
    $doctors = Doctor::where('polikliniks',Auth::user()->id)->orderBy('id','DESC')->get();
    return view('panel.doctors',['doctors'=>$doctors]);
}

My blade is:

@foreach($doctors as $doctor)
      <tr id="sid{{$doctor->id}}">    
           <input type="hidden" value="{{$doctor->id}}" name="id">                    
           <td>
                <p>{{$doctor->name}}</p>
           </td>
      </tr>
@endforeach

My ajax is:

  $('#doctorForm').submit(function(e){
        e.preventDefault();

        let id = $('#id').val();
        let name = $("#name").val();

        $.ajax({
            url: "{{route('add_doctorPost')}}",
            type:"POST",
            data:{id:id,name:name,_token:'{{ csrf_token() }}'},
            success:function(response){
                if(response){
                    $("#doctorTable tbody").prepend('<tr><td>p>'+ response.name +'</p></td><td><button type="button" class="btn btn-primary"  data-toggle="modal" data-target="#appointmentModal{{$doctor->id}}" ></button><td><a href="javascript:void(0)" data-type="confirm" class="btn btn-danger" onclick="deleteConfirmation({{$doctor->id}})" </a></td>  </tr>')
                    $('#doctorForm')[0].reset();
                    $('addDoctor').modal('hide');
                }
            }
        });


    });
0

2 Answers 2

1

The issue is with the following excerpt from the line you identified:

'... data-target="#appointmentModal{{$doctor->id}}" ...'

The problem here is that when your blade.php file is being rendered by PHP, the PHP moustache expressions (here {{$doctor->id}}) is being evaluated, and because it doesn't have any $doctor variable defined this will throw an exception. You might have taken this from another part of your blade file, but because you are getting data back via AJAX, this should most likely be a javascript variable (like you have done with response.name) instead of a PHP variable.

e.g.

'... data-target="#appointmentModal' + id + '" ...'

Based on your code I'm assuming you are using this expression to reference a unique modal for that doctor, and I am also assuming that you are generating these modals using a blade @foreach. In which case, in your response you would need to create the new modal too.

Sign up to request clarification or add additional context in comments.

Comments

0

Is because you're referencing $doctor really out of the context, what you wanna do is this:

$('#doctorForm').submit(function(e){
        e.preventDefault();

        let id = $('#id').val();
        let name = $("#name").val();

        $.ajax({
            url: "{{route('add_doctorPost')}}",
            type:"POST",
            data:{id:id,name:name,_token:'{{ csrf_token() }}'},
            success:function(response){
                if(response){
                    $("#doctorTable tbody").prepend('<tr><td>p>'+ response.name +'</p></td><td><button type="button" class="btn btn-primary"  data-toggle="modal" data-target="#appointmentModal"' + id +  '  ></button>  </tr>')
                    $('#doctorForm')[0].reset();
                    $('addDoctor').modal('hide');
                }
            }
        });


    });

I guess replacing your $doctor->id by the let id you defined.

2 Comments

i updated my ajax script can you look again? i have two dynamic variable one of them coming from function and the other is data-target.
You´re still referencing your $doctor variable into that block of javascript,, why don you reference the let id directly?the only way that'd work would be if you return one record to de the veiw, by that I mean replacing the array $doctors by $doctor

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.