php 以模态形式保存已编辑的注解的问题

k3bvogb1  于 2023-05-21  发布在  PHP
关注(0)|答案(2)|浏览(141)

我目前面临着一个问题,即在我的网站上以模态形式保存编辑过的笔记。我有一个允许用户编辑他们的笔记的模式,但是当他们点击“保存更改”时,更新的笔记没有被正确保存。它将其保存为null。注解为空,表示所需的编辑未保存在数据库中
下面是我的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>
                                         <form id="saveEditsForm"  action="#" 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>

这是我的控制器中的代码

// the following function is to edit a note
     public function editNote($note_id) {
    $note = notes::find($note_id);
    return response()->json([
        'noteContent' => $note->note
    ]);
}

  public function editNoteForm($note_id)
{
   $note = notes::find($note_id);

if (!$note) {
    // Handle the case when the note is not found
    return redirect()->back()->with('error', 'Note not found!');
}

return view('modal') -> with ('note',$note);
}

 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.');

我的路线

Route::get('edit-note/{note_id}', [NotesController::class, 'editNoteForm'])->name('edit-note-form');

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

ruyhziif1#

这是因为您的'note_content[]'字段不在'saveEditsForm'表单中。

wn9m85ua

wn9m85ua2#

你有这个:

@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>

但是你的笔记信息是form,你有其他更高的形式。我不太明白你为什么要用隐藏输入来更新笔记。如果希望用户编辑此注解,也许可以用途:

<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>

相关问题