2 回答

TA貢獻1825條經驗 獲得超4個贊
對于簡單的方法,我推薦你使用 jQuery 的 Ajax,這樣你不需要做太多的改變,為 API 添加一個路由,檢查數據庫中的數據和幾行 JavaScript 代碼。您可以嘗試以下方法:
控制器:
use Your\Namespace\Path\Of\Model Model
Class ABC {
...
public function checkExist(){
$data = request()->get('data'); // get data to check
$model = new Model();
$where = ['id' => $data]; // passing your where condition to check
return $model->where($where)->get()->count() > 0;
}
}
路由文件(web.php/api.php):
路線::get('checkExist', 'ABC@checkExist');
看法
<form action"/send" method="post">
<div id="info1">
<!-- info about first item -->
</div>
<div id="info2">
<!-- info about second item -->
</div>
<div id="info_default">
<!-- if customised item is chosen, show this one -->
</div>
<div id="info_does_not_exist">
<!-- if cannot find the input item in database, show this one -->
</div>
<button type="submit">submit</button>
</form>
...
<script>
$(function(){
// every time input change, check value existence in database
$('.info2 input').change(function(){
let input_value = $(this).val();
$.ajax({
url: "{{ route('checkExist') }}",
method: "GET",
data: {"id": input_value},
success: function(response){
// process if/else to show div '.info_default' / '.info_does_not_exist'
if (response) {
// show '.info_default' and hide '.info_does_not_exist'
} else {
// show '.info_does_not_exist' and hide '.info_default'
}
}
});
})
})
</script>
這不是實際的可運行代碼,只是一個想法,根據這個,您可以根據您的進一步需求對其進行調整。希望這會有所幫助

TA貢獻2016條經驗 獲得超9個贊
您必須使用 AJAX 之類的東西來檢查數據庫中的用戶輸入值,然后做出正確的決定,在添加或顯示數據庫中的內容之前不顯示任何內容。
因此工作流程如下:用戶輸入 => ajax 接受用戶輸入并提交給控制器函數以檢查數據庫是否存在值 => 取決于從該控制器函數返回的值 => 向用戶顯示正確的值
添加回答
舉報