You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel模态框输入值传递问题:控制器无法获取numexam参数

Hey there! Let's break down why your numexam value is showing up as null in your controller and fix it step by step.

The Core Issue

Your button's onClick event is updating the innerHTML of the hidden input, but form inputs use the value attribute to pass data when submitted. Changing the innerHTML doesn't affect the input's actual submitted value—so it stays empty, hence the null in your controller.

Fixes & Improvements

1. Correct the Button's OnClick Logic

Update the button to set the hidden input's value instead of its innerHTML:

<td>
  <button value="{{$verification->numexam}}" onClick="document.getElementById('numexam').value=this.value" type="button" class="btn btn-light" data-toggle="modal" data-target="#exampleModalCenter">
    Update mark
  </button>
</td>

2. Switch to POST for Data Modification (Best Practice)

Since you're updating data, using a POST request aligns with RESTful conventions and Laravel's CSRF protection (which is required for non-GET requests).

Update your route in web.php:

Route::get('/ajout', 'ExamsController@create');
Route::post('/ajout', 'ExamsController@store')->name('exam.import');
Route::post('/notemodifier', 'ExamsController@updatenote'); // Changed from GET to POST

3. Clean Up the Form

Use Laravel's @csrf Blade directive instead of manually writing the CSRF token, and set the form method to post:

<form action="/notemodifier" method="post">
  @csrf <!-- Replaces the manual CSRF hidden input -->
  <div class="modal-content">
    <div class="modal-header">
      <h5 class="modal-title" id="exampleModalLongTitle">Modifier la note</h5>
      <button type="button" class="close" data-dismiss="modal" aria-label="Close">
        <span aria-hidden="true">&times;</span>
      </button>
    </div>
    <div class="modal-body">
      <center>
        <label>Taper la nouvelle note:</label>
        <input required type="text" name="note">
      </center>
      <input type="hidden" name="numexam" id="numexam">
    </div>
    <div class="modal-footer">
      <button type="button" class="btn btn-secondary" data-dismiss="modal">Fermer</button>
      <button type="submit" class="btn btn-primary">Modifier</button>
    </div>
  </div>
</form>

4. Optional: Enhance Controller Logic

Add validation and error handling to make your code more robust:

public function updatenote(Request $request){
    // Validate incoming data
    $validated = $request->validate([
        'numexam' => 'required|integer',
        'note' => 'required|numeric', // Adjust based on your note's data type
    ]);

    $num = $request->numexam;
    // dd($num); // This should now return the correct value!

    // Check if the exam exists to avoid errors
    $exam = Exam::find($num);
    if (!$exam) {
        return back()->with('error', 'Exam not found');
    }

    $exam->note = $request->note;
    $exam->save();

    return Redirect::back()->with('success', 'Note updated successfully');
}

After making these changes, your numexam value should be correctly passed to the controller, and the update functionality will work as expected.

内容的提问来源于stack exchange,提问作者Mehdi El Aissi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 07:04:37