michael_bt's avatar

how to pass data image location using jQuery and show it in modal

hello, im trying to show photo when i click a button like data did. this is my view

@foreach ($users as $user)
 <td>{{$user->name}}</td>
 <button class="btn-success showuser" 
  data-id="{{ $user->id }}" 
  data-userphoto="{{ $user->user_photo }}"
  data-toggle="modal" data-target="#modal-insert">
 <i class="fa fa-pencil"  align="center">Insert Proof</i>
 </button>
@endforeach

$(document).ready(function(){
    //Upload
    $(document).on('click', '.showuser', function() {
    $('#iduser').val($(this).data('id'));
    $('#userphoto').val($(this).data('userphoto'));
    });

});

modal insert

<div class="modal fade" id="modal-insert">
<div class="modal-dialog" role="document">
<div class="form-group">
 <input type="number" name="id" id="iduser">
 </div>
 <div class="form-group">
 <input type="file" class="form-control" name="user_photo" id="userphoto">
 <img style="max-width: calc(100% - 20px)" src="/storage/(**contain id="userphoto"**)">
</div>

how to pass the value of id="userphoto" into the src="/storage/**" ?

0 likes
2 replies
burlresearch's avatar
Level 40

I'm a little confused by what I'm seeing, but I think I would:

  1. put the click on the button
  2. presume that the data-userphoto is the full url (?!)
  3. then use jQuery to replace the img.src property in the modal:
$('.showuser').click(function () {
  $('.form-group img').prop('src', $(this).data('userphoto'));
});

no?

1 like
michael_bt's avatar

Sorry sir im not clear

So im using this function to store the data image

public function insertproof(Request $request)
    {
        $id = $request['id'];
        if ($request->hasFile('rent_proof')){
            $filepath = $request->file('rent_proof')->store('public');

         
        $result = rent::where('id',$id)->first();
        $result->rent_proof=$filepath;
        $result->save();
        
        }
        return redirect()->back()->with('status', 'proof updated');
    }

this function gives me 'public/{filename}' on database and it saved at

storage/app/public

with storage:link it also gives me

public/storage

according to documentation, i can use something like this

{{ 'storage/'.$rent->rent_proof }}

but i cant seems to load it through my view because i have id as the data

                          <button class="btn-success editrent" 
                          data-id="{{ $rent->id }}"
                          data-proof="{{ $rent->rent_proof }}"//this one filepath
                          data-toggle="modal" data-target="#modal-insertproof">
                          <i class="fa fa-pencil"  align="center">Input proof</i>
                          </button>

and this is my js

$(document).ready(function(){
        $(document).on('click', '.editrent', function() {
        $('#idrent').val($(this).data('id'));
        $('#rentproof').val($(this).data('proof'));//filepath
        });

my modal after i click

       <form method="POST" action="{{route('visitor_rent.insertproof')}}" enctype="multipart/form-data">//use the insertproof controller
            <div class="form-group">
                <input type="file" class="form-control" name="rent_proof"  id="rentproof" >
                <img style="max-width: calc(100% - 20px)" src="**this is where im confused**">
            </div>
    </form

That is maybe the full question,but you were in right direction for guide me, even though im not giving spesific question.sorry for that sir. GBU and thanks sir.

So i got Solution now

$('#userphoto').attr("src"',"/storage/"+$(this).data('userphoto'));

Use this on modal trigger

and then we can pass id on the <img

Please or to participate in this conversation.