复选框状态不会持续存在于表单提交中

时间:2015-01-06 03:57:54

标签: php forms

我有一系列复选框,当表单提交并且出现错误(通过jquery验证)时,我需要显示这些复选框的状态,以便不必重新输入:

<fieldset class="group">
        <label for="s_type" class="mandatory full-width <?=$errors['s_type']?>"><strong>Please indicate which you are applying for:</strong></label>
        <span class="clear"></span>
    </fieldset>
    <fieldset class="group">
        <div class="field">
            <input <?php if($s_type == "Fund Scholarship") { echo 'checked';} ?> type="checkbox" value="Fund Scholarship" name="s_type[]" id="s_type_fund" validate="required:true">
            <label>Fund Scholarship</label><br/>
            <input <?php if($s_type == "Foundation Scholarship") { echo 'checked';} ?> type="checkbox" value="Foundation Scholarship" name="s_type[]" id="s_type_foundation">
            <label>Foundation Scholarship</label><br />
            <input <?php if($s_type == "Memorial Scholarship") { echo 'checked';} ?> type="checkbox" value="Memorial Scholarship" name="s_type[]" id="s_type_mem">
            <label>Memorial Scholarship</label><br />
            <label class="error" for="s_type[]">This field is required</label></div>
    </fieldset>

出于某种原因,当我选择其中一个选项时,它只保存,如果我选择2或3,它不会保存?不确定我错过了什么

编辑:哦,我应该提一下,当选择多个时,它会失去所有状态,即使我选择一个状态,它也能保持正确。

编辑:我刚刚意识到代码顶部有以下内容:

$s_type = implode("; ", $_POST['s_type']);

我认为这就是为什么$ s_type ==“基金奖学金”据说可以在我用来模拟这个的另一个php页面上工作

1 个答案:

答案 0 :(得分:0)

$s_type一次只能等于一件事。

如果您想使用所有这些,请尝试in_array()

<input <?php if(in_array("Foundation Scholarship",$_REQUEST['s_type'])) echo 'checked'; ?> type="checkbox" value="Foundation Scholarship" name="s_type[]" id="s_type_foundation">
相关问题