【问题标题】:Laravel 4 how do I keep checkbox checked after form is submittedLaravel 4如何在提交表单后保持复选框选中
【发布时间】:2013-11-29 01:31:50
【问题描述】:

在我的编辑表单的一部分中,我有复选框。我可以很好地提交表单,这些值作为逗号分隔值存储在数据库中。所以苹果,微软等。哪个工作正常但是当我回到编辑表单时,复选框没有被选中。如果满足以下条件,我尝试回显检查:

<input name="software[]" type="checkbox"  value="Ortho trac" <?php if(isset($_POST['software'])) echo "checked"; ?>> Ortho trac

但是上面的代码不起作用。我不知道如何在 laravel 中做到这一点。 当我尝试更新值时,数据库中的先前值消失了

观点

<div class="form-group mtop-20">

    <label for="temp_software_experience">Software Experience:</label><br>
    <label class="checkbox-inline">
        <input name="software[]" type="checkbox" class="input-sm" value="Practiceworks"> Practiceworks
    </label>
    <label class="checkbox-inline">
        <input name="software[]" type="checkbox" value="Softdent"> Softdent
    </label>
    <label class="checkbox-inline">
        <input name="software[]" type="checkbox" value="Ortho trac" <?php if (isset($_POST['software'])) echo "checked"; ?>> Ortho trac
    </label>
    <label class="checkbox-inline">
        <input name="software[]" type="checkbox" value="Dentrix"> Dentrix
    </label>
    <label class="checkbox-inline">
        <input name="software[]" type="checkbox" value="Easy Dental"> Easy Dental
    </label>
    <label class="checkbox-inline">
        <input name="software[]" type="checkbox" value="Practiceworks"> Eaglesoft
    </label>
    <label class="checkbox-inline">
        <input type="checkbox" id="temp_software_experience" value="Other"> Other
    </label>

</div><!-- end .form-group -->

控制者

public function update($id)
{

    // validate
    // read more on validation at http://laravel.com/docs/validation
    $rules = array(
        'firstname' => 'required|min:3|alpha',
        'lastname' => 'required|min:3|alpha',
        'zipcode' => 'required|min:5|numeric',
        'temp_experience' => 'required|min:1|max:50|numeric',
        'temp_travel' =>   'required|numeric',
        'temp_hourly_rate' => 'required|numeric|min:10',
    );
    $validator = Validator::make(Input::all(), $rules);

    // process the login
    if ($validator->fails()) {
        return Redirect::to('user/profile/' . $id . '/edit')
            ->withErrors($validator)
            ->withInput();
    } else {


        $software_checked = Input::get('software');
        if(is_array($software_checked))
        {
            $implade_software = implode(',', $software_checked);
        }

        // store
        $user = User::find($id);
        $user->firstname       = Input::get('firstname');
        $user->lastname      = Input::get('lastname');
        $user->zipcode = Input::get('zipcode');
        $user->temp_experience = Input::get('temp_experience');
        $user->temp_travel = Input::get('temp_travel');
        $user->temp_hourly_rate = Input::get('temp_hourly_rate');
        $user->temp_software_experience = $implade_software;
        $user->save();

        // redirect
        Session::flash('message', 'Successfully updated profile!!');
        return Redirect::to('user/profile/'.$id.'');
    }

}

【问题讨论】:

    标签: php laravel-4


    【解决方案1】:

    当您更新时,您会在之后进行重定向,从而清除您的 POST 数据。

    // redirect
    Session::flash('message', 'Successfully updated profile!!');
    return Redirect::to('user/profile/'.$id.'');
    

    要访问此数据,您需要从您的存储方法(在本例中为数据库)获取它,而不是从您的 POST 数据中获取,因为它不包含任何内容。

    所以条件应该是这样的:

    <?php if(!empty($user->software)) echo "checked"; ?>
    

    注意:即使在视图中,您也应该使用Input::get(),使用$_POST 没有意义。

    从 cmets 编辑:

    您丢失数据的原因是因为您首先需要explode() 数据,然后再对它执行条件检查。

    假设您选择:

    [x] Apple
    [ ] Orange
    [x] Banana
    

    在您的专栏中,它看起来像 apple,banana

    当您执行条件检查时,它看起来类似于:

    if ('apple,banana' == 'apple') echo 'checked';
    

    您需要检查该逗号分隔列表中的元素是否存在。所以尝试先爆炸,然后检查。这应该是一个很好的起点。

    $software = explode(',', $user->temp_software_experience);
    

    然后对于每个输入:

    if (in_array("Practiceworks", $software)) echo "checked";
    

    这是in_array() 的文档。

    希望能解决一些问题。

    【讨论】:

    • 这很好用 temp_software_experience) && $user->temp_software_experience == "Practiceworks") echo "checked"; ?> 当我返回编辑页面时,它会显示选中的框。当用户检查一项附加技能时,我能做些什么吗?因为现在当我检查另一个复选框时,没有一个复选框被选中,并且之前选择的值不在数据库中@FruityP
    • 感谢您的帮助,它现在按预期工作。希望你享受你的假期@FruityP
    猜你喜欢
    • 2013-08-10
    • 2015-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-20
    • 1970-01-01
    • 2017-08-30
    相关资源
    最近更新 更多