skip to Main Content

I’m currently facing an issue with saving edited notes in a modal form on my website. I have a modal that allows users to edit their notes, but when they click "Save changes," the updated note is not being saved correctly. It saves it as null. the note is then empty the desired edits did not save in the DB

Here’s the structure of my modal:

 <div class="modal fade" id="noteModal">
                                    <div class="modal-dialog" role="document">
                                        <div class="modal-content">
                                            <div class="modal-header">
                                                <h5 class="modal-title">Edit Notes</h5>
                                                <button type="button" class="close" data-dismiss="modal"><span>&times;</span>
                                                </button>
                                            </div>
                                           
                                            <div class="modal-body">

                                            
            <form id="editNoteForm" action="#" method="POST" >
                @csrf
                @method('PUT')
              
                
                <textarea name="note_content" id="noteContent" cols="30" rows="5" class="form-control bg-transparent" title="Please edit your note" required> </textarea>
            </form>
                              
     

                                            </div>
                                            @foreach ($notes as $note)
    <input type="hidden" name="note_content[]" value="{{ $note->note }}">
@endforeach
                                            <form id="saveEditsForm"  action="{{ route('saveEdits', ['note_id' => $note->note_id]) }}" method="POST">
                                            @method('PUT')
                                            @csrf




                                            <div class="modal-footer">
                                                <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                                                <button type="submit" class="btn btn-primary">Save changes</button>
                                            </div>
                                            </form>

                                            
                                        </div>
                                    </div>
                                </div>  

here is the code in my controller

 public function saveEdits(Request $request, $note_id)
 {
    $noteContent = $request->input('note_content');

$note = notes::find($note_id);

if (!$note) {
    return redirect()->back()->with('error', 'Note not found!');
}

     $note->note = $noteContent;
$note->save();

return redirect()->back()->with('success', 'Note updated successfully.');                                 

my route

 Route::put('save-edits/{note_id}', [NotesController::class, 'saveEdits']) -> name('saveEdits');

2

Answers


  1. It is happening because your ‘note_content[]’ field is not inside the ‘saveEditsForm’ form.

    Login or Signup to reply.
  2. you have this:

    @foreach ($notes as $note)
        <input type="hidden" name="note_content[]" value="{{ $note->note }}">
    @endforeach
    <form id="saveEditsForm"  action="{{ route('saveEdits', ['note_id' => $note->note_id]) }}" method="POST">
        @method('PUT')
        @csrf
    
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="submit" class="btn btn-primary">Save changes</button>
      </div>
     </form>
    

    But your note info it´s out of form and you have other form higher. I don´t understan very well why you ar using hidden input to update notes. if you want that user edit this notes, maybe you can use:

    <form id="saveEditsForm"  action="{{ route('saveEdits', ['note_id' => $note->note_id]) }}" method="POST">
        @method('PUT')
        @csrf
    
        <textarea id="w3review" name="w3review" rows="4" cols="50">
           @foreach ($notes as $note)
             {{ $note->note }}"
           @endforeach
        </textarea>
    
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="submit" class="btn btn-primary">Save changes</button>
      </div>
     </form>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search