You need to enable JavaScript to run this app. Laravel发起POST请求后如何将返回数组值填充到模态框中
优惠活动
大模型
产品
解决方案
定价
更多

Laravel发起POST请求后如何将返回数组值填充到模态框中

Laravel 预约表单时段渲染需求说明

我正在开发一款预约表单,支持用户选择想要预约的日期与时间范围。我希望点击搜索按钮后,将请求得到的结果数组展示在模态框中,通过@forelse ($dt as $dt)语法完成数据填充渲染。

@forelse ($dt as $dt).


blade.php 视图页面代码

@section('content')
    <div class="card-body">
        <form action="{{ route('assistant.newappts.new') }}" method="POST">
            @method('post')
            @csrf
            <label for="select_date">选择日期:</label>
            <input type="text" id="select_date" class="form-control" name="select_date" placeholder="选择日期">
            <div class="spacer"></div>
            <div class="half-form">
                @foreach ($operatinghrs as $operatinghr)
                <div class="form-group">
                    <label for="starttime">选择开始时间 </label>
                    <br>
                    <input type="time" id="start_time" name="start_time" min="{{display24HTime($operatinghr->start_time)}}" max="{{display24HTime($operatinghr->end_time)}}" value="{{ display24HTime(old('start_time', $operatinghr->start_time))}}">
                </div>
                <div class="form-group">
                    <label for="starttime">选择结束时间</label>
                    <br>
                    <input type="time" id="end_time" name="end_time" min="{{display24HTime($operatinghr->start_time)}}" max="{{display24HTime($operatinghr->end_time)}}" value="{{ display24HTime(old('end_time', $operatinghr->end_time))}}">
                    <div class="form-update" style="float:right;">
                        <button type="button" class="btn btn-primary" data-toggle="modal" id="find_appointment_btn">搜索</button>
                    </div>
                </div>
                @endforeach             
            </div>
            <div class="spacer"></div>
        </form>
    </div>
@overwrite

@section('modal')
<!-- 新建预约模态框 -->
<div class="modal fade" id="myModalNewAppointment">
    <div class="modal-dialog">
        <div class="modal-content">
            <!-- 模态框头部 -->
            <div class="modal-header">
                <h4 class="modal-title">选择预约时间</h4>
            </div>
            <!-- 模态框内容 -->
            <div class="modal-body">
                <div class="form-group">
                    <div class="timeslots-section">
                        <div class="timeslots text-center">
                            @forelse ($dt as $dt)
                                <div class="timeslot">
                                    <input type="checkbox" class="timeslotchk" name="timeslot" value="{{$dt}}">
                                    {{$dt}}
                                </div>
                            @empty
                            <div class="empty" style="text-align: left">暂无可用预约时段。</div>
                            @endforelse
                        </div> <!-- 时段列表结束 -->
                    </div>
                </div>
               
            </div>

            <!-- 模态框底部 -->
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" id="new_appt_save_btn" data-loading-text="<i class='fa fa-spinner fa-spin'></i> 保存中..">保存</button>
                <button type="button" class="btn btn-outline-primary" id="close_btn" data-dismiss="modal">关闭</button>
            </div>

        </div>
    </div>
</div>
@overwrite

页面对应JavaScript代码

<script>
    $(document).ready(function() {
        $('#select_date').datepicker({      
            dateFormat: "dd-mm-yy",
            changeMonth: true,
            changeYear: true,
            todayHighlight: true,
            toggleActive: true
        }); 

        $('#find_appointment_btn').click(function() {
            $('#myModalNewAppointment').modal({backdrop: 'static', keyboard: false});
            $('#myModalNewAppointment .modal-title').html('新建预约');
        });
    })

</script>

控制器处理方法

public function index()
    {
        if ($_SESSION['user_role'] != 'Assistant') {
            return "无访问权限";
        }
        $workinghr = WorkingHour::where('clinic_id', $_SESSION['clinic_ID'])->where('off_day', false)->pluck('day')->toArray();
        
        $todayday = Carbon::now('Asia/Singapore')->format('l');
        $operatinghrs = $this->getWorkDayTiming($todayday);

        $data = [
            'link_menu_appt' => 'open active',
            'link_newappt' => 'active',                       
            'operatinghrs' => $operatinghrs,            
        ];

        return view('patient_appointment.available-appointment', $data);
    }

    public function filterByDate(Request $request)
    {
        $dateselected = $request->select_date;
        $starttime = $request->start_time;
        $endtime = $request->end_time;
        
        $dtarray = $this->getSelectedDate($dateselected, $starttime, $endtime);
        $dt = array_slice($dtarray, 0, 12, true);

        $todayday = Carbon::now('Asia/Singapore')->format('l');
        $operatinghrs = $this->getWorkDayTiming($todayday);
        
        $data = [
            'link_appt' => 'active',                   
            'operatinghrs' => $operatinghrs,       
            'dt' => $dt,                
        ];

        return view('patient_appointment.available-appointment', $data);

    }

内容的提问来源于stack exchange,提问作者junmingyeo98

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.21 23:06:05