簡體   English   中英

避免表單數據在表單提交時變得清晰

[英]avoid form data getting clear on form submission

我正在使用 html 表單使用 laravel php 獲取用戶數據。在表單提交上我正在執行一個 api,它在 Web.php 中被定義為

Route::post('/register_user', 'UserRegistrationController@registerUser'); 

哪個執行,

public function registerUser(Request $request){
    
    $apiResult = $this->executeApi($request->input('mobile_no'));
      
    if($apiResult === "Success"){
   
            return view('further_view');

    }else{

            toastr()->error('Registration Failed. Please check mobile number.');
            return back();
    }

}

html代碼

 <div class="form-group{{ $errors->has('mobile_no') ? ' has-error' : '' }}">
                             <label for="mobile_no">Mobile No</label>
                             <input type="text" name="mobile_no" class="form-control" id="mobile_no" value="{{ old('mobile_no') }}" placeholder="Enter Mobile No" required>
                             <small class="text-danger">{{ $errors->first('mobile_no') }}</small>
                        </div>

我的問題是,每當我的 $apiResult === "Success" 條件變為假時,我會在屏幕上收到 toast 消息,但我的頁面會刷新,並且用戶輸入的所有數據都會從輸入框中清除。 因此,我的問題是如何在不清除表單的情況下顯示吐司消息,或者如何防止在這種情況下清除輸入框。

我正在使用這個 Toast 庫https://github.com/yoeunes/toastr

我建議您使用 session 來存儲數據。 例如:

session()->put('forms.mobile_no', $request->get('mobile_no'));

您的 HTML:

<input value="{{ $mobile_no }}" type="text" name="mobile_no" class="form-control" id="mobile_no" placeholder="Enter Mobile No" required>

嘗試在 toastr 調用后返回錯誤消息:

return redirect->back()->withInput();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM