【问题标题】:How to get dynamic generated input id and values如何获取动态生成的输入 id 和值
【发布时间】:2018-08-23 05:02:48
【问题描述】:

我有一个页面,允许编辑会议注册的注册表单。在此表单中,有一个表格显示该会议存在的问题,然后用户可以通过复选框将每个问题与注册类型相关联。

选中复选框后,他可以点击更新按钮更新注册表。

怀疑:

我的疑问是在用户单击“更新”按钮后如何更新注册表单,同时考虑到选中的复选框。复选框 id 和 value 是动态的,存储在注册表类型表中。那么如何在更新方法中获取选中的复选框,以便可以根据选中的复选框更新registration_type_questions数据透视表?

问题控制者:

// returns the view to edit the conference registration form
        public function edit($id)
        {
            $conference = Conference::find($id);
            $registrationType = RegistrationType::where('conference_id', $id)->get();
            $question = Question::where('conference_id', $id)->get();

            return view('questions.edit')
                ->with('conference', $conference)
                ->with('registration_type', $registrationType)
                ->with('question', $question);
        }
    }
    // update the registration form based on checkboxes selected
     public function update(Request $request, $id){

        $this->validate($request, [
        // ??
        ]); 

        // ??
     }

RegistrationType 模型:

class RegistrationType extends Model
{
    protected $fillable = [
        'name','conference_id',...
    ];
    public function conference(){
        return $this->belongsTo('App\Conference');
    }
    public function questions(){
        return $this->belongsToMany('App\Question', 'registration_type_questions');
    }
}

问题模型:

class Question extends Model
{
    protected $fillable = [
        'question', 'type', 'conference_id',
    ];

    public function registration_type(){
        return $this->belongsToMany('App\RegistrationType', 'registration_type_questions');
    }
}

编辑会议登记表代码:

<form id="edit_registration_types" method="post" class="clearfix" action="{{route('rtypes.update', ['conf_id' => $conf->id])}}">

    {{csrf_field()}}

    <div class="form-row">
        <div class="form-group col">
            <div class="row">
                <div class="col">
                    <table class="table table-striped table-responsive-sm">
                        <thead>
                        <tr>
                            <th scope="col">Info</th>
                            <th scope="col">Include in registration type</th>
                            <th scope="col">Mandatory Field</th>
                        </tr>
                        </thead>
                        <tbody>
                        <tr>
                            <td>Name</td>
                            <td>
                                <i class='fa fa-check font-size-lg text-primary ml-4'></i>
                            </td>
                            <td>
                                <i class='fa fa-check font-size-lg text-primary ml-4'></i>
                            </td>
                        </tr>
                        <tr>
                            <td>Surname</td>
                            <td>
                                <i class='fa fa-check font-size-lg text-primary ml-4'></i>
                            </td>
                            <td>
                                <i class='fa fa-check font-size-lg text-primary ml-4'></i>
                            </td>
                        </tr>



                        @foreach($question as $q)
                            <tr>
                                <td>{{$q->question}}</td>
                                <td>
                                    @foreach($registration_type as $rtype)
                                        <div class="form-check">
                                            <input autocomplete="off" class="form-check-input" type="checkbox" value="{{ $rtype->id }}" id="{{$rtype->id}}">
                                            <label class="form-check-label" for="exampleRadios1">
                                                {{$rtype->name}}
                                            </label>
                                        </div>
                                    @endforeach
                                </td>
                                <td>
                                    @foreach($registration_type as $rtype)
                                        <div class="form-check">
                                            <input autocomplete="off" class="form-check-input" type="checkbox" value="{{ $rtype->id }}" id="{{$rtype->id}}">
                                            <label class="form-check-label" for="exampleRadios1">
                                                for the registration type "{{$rtype->name}}"
                                            </label>
                                        </div>
                                    @endforeach
                                </td>
                            </tr>
                        @endforeach

                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
    <input type="submit" class="btn btn-primary float-right mt-3" value="Update"/>
</form>

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    据我所知,您只是想要一种从前端捕获动态复选框信息的简单方法。如果是这样,那么您可以简单地使用为此构建的 html 命名约定。例如mycheckbox[]

    将其用作所有复选框名称,并在后端处理值,即

    @foreach($registration_type as $rtype)
        <div class="form-check">
            <input name="mycheckbox[]" autocomplete="off" class="form-check-input" type="checkbox" value="{{ $rtype->id }}" id="{{$rtype->id}}">
            <label class="form-check-label" for="exampleRadios1">
               {{$rtype->name}}
            </label>
        </div>
    @endforeach
    

    后端:

    // update the registration form based on checkboxes selected
    public function update(Request $request, $id){
        $values = $request->get('mycheckbox');
        foreach($values as $rtype_id){
            //do whatever you want with this rtype id that was checked
    

    【讨论】:

    • 谢谢。我试图这样做,我需要在registration_type_questions 数据透视表中插入question_id,即所选复选框的registration_type_id。但我没有这张桌子的模型。那么你知道如何插入数据透视表吗?是否可以根据选中的复选框更新表单?
    • 但我不知道它是否正常工作,使用这种方法 "public function update(Request $request, $id){ $values = $request->get('rtypes'); dd ($values); }",如果所有复选框都被选中,则显示为 "array:2 [▼ 0 => "1" 1 => "2" ]"。
    • 值是$rtypes,所以你可以简单地通过RegistrationType::find($rtype_id)找到registration_type_id
    • 谢谢,但是注册类型的 id 已经在视图中。问题是如何获取所选复选框的这些注册类型 ID,并将所选复选框的registration_type_id 和每个问题的question_id 存储在数据透视表“registration_type_questions”中。
    • @JohnZ 冷静下来,再次阅读我的答案。 rtypes 是您在值转储中获得的 1 和 2
    猜你喜欢
    • 2018-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-20
    相关资源
    最近更新 更多