Mike_SA's avatar

File upload within a foreach dataset saves image in wrong dataset

Hi,

I am delving into an area I normally dont play in :) Unfortunately today I have an employee that is sick whom normally handles this.

The feature I am adding is to change the current set image and upload a new on... Currently everything is working perfectly except upon saving. The image file is in the wrong position in the array and saves to the first data within the foreach instead of the data set that was updated.

I need to do to record the position and add it to the array of images in the correct place but not well versed enough on JS... could someone please point me in the right direction..

Thank you in advance

Running laravel 5.8, jQuery 3.4...

Foreach loop

@foreach($page->profile->profiles as $slider => $value)

                                <tr class="row1 removeHere" data-id="{{ $i }}">

                                    <td class="col-md-3">
                                        <div id="changed-images-wrap" class="align-self-center">
                                            <div id="changeImage" class="buttonParent">
                                                <img src="{{ asset($value['image'])}}" alt="" style="width: 100%;">

                                                <button id="changed_image" class="btn btn-danger text-white"
                                                        type="button" onClick="removeHTMLwithinIDchangeImage(this)"
                                                        style="width: 100%; margin-top: 15px;">Change image
                                                </button>

                                            </div>

                                            <!-- Required to have the correct count in foreach -->
                                            <input type="hidden" name="profile_imagesPrevious[]"
                                                   value="{{ isset($value['image']) ? $value['image'] : null }}">




                                        </div>


                                    </td>
                                    <td class="col-md-7">
                                        <div class="form-group">
                                            <label for="name" class="control-label col-md-12">Name</label>
                                            <div class="col-md-12">
                                                <input type="text" name="profile_name_previous[]"
                                                       class="form-control"
                                                       value="{{ isset($value['name']) ? $value['name'] : null }}">
                                            </div>
                                        </div>

                                        <div class="form-group">
                                            <label for="name" class="control-label col-md-12">Position</label>
                                            <div class="col-md-12">
                                                <input type="text" name="profile_position_previous[]"
                                                       class="form-control"
                                                       value="{{ isset($value['position']) ? $value['position'] : null }}">
                                            </div>
                                        </div>

                                        <div class="form-group">
                                            <label for="name" class="control-label col-md-12">Content</label>
                                            <div class="col-md-12">

                                                {!! Form::textarea('profile_content_previous[]', isset( $value['content']) ? $value['content'] : null , ['class' => 'form-control small']) !!}

                                            </div>
                                        </div>

                                        <div class="form-group">
                                            <label for="name" class="control-label col-md-12">Email</label>
                                            <div class="col-md-12">
                                                <input type="text" name="profile_email_previous[]"
                                                       class="form-control"
                                                       value="{{ isset($value['email']) ? $value['email'] : null }}">
                                            </div>
                                        </div>

                                        <div class="form-group">
                                            <label for="name" class="control-label col-md-12">Twitter</label>
                                            <div class="col-md-12">
                                                <input type="text" name="profile_twitter_previous[]"
                                                       class="form-control"
                                                       value="{{ isset($value['twitter']) ? $value['twitter'] : null }}">
                                            </div>
                                        </div>

                                        <div class="form-group">
                                            <label for="name" class="control-label col-md-12">Linkedin</label>
                                            <div class="col-md-12">
                                                <input type="text" name="profile_linkedin_previous[]"
                                                       class="form-control"
                                                       value="{{ isset($value['linkedin']) ? $value['linkedin'] : null }}">
                                            </div>
                                        </div>


                                    </td>
                                    <td class="col-md-1">
                                        <button class="btn-remove-div btn btn-primary btn-purple align-middle">Remove
                                            image
                                        </button>
                                    </td>
                                </tr>

                                @php
                                    $i++;
                                @endphp
                            @endforeach 

Script

<script>
function removeHTMLwithinIDchangeImage(el) {
              var $template_name = $(".template_nm").val();

              var appendUpload = (el).closest('div');
              console.log(appendUpload);

              appendUpload.innerHTML = '<div class="AddNewImageProfile"><label for="featured_image">Change image:</label><p style="font-size: 10px;">New image will only show after the page has been saved</p>' +
                  '<input name="profile_imagesPreviousNew[]" type="file" id="fileUploadProfilePrevious uploadMsg" onchange="FChange(this)"></div>';
          }

            </script>
'''
0 likes
0 replies

Please or to participate in this conversation.