在smarty中提交表单后选中复选框


Checkbox checked after form submit in smarty

我在smarty中编码,在我的表单中有很多复选框,当我检查它时,然后在DB中正确提交它,但我希望:当我下次打开表单时,"它应该保持检查"如前所述。但我的表单不像这个功能那样工作。我有.tpl文件。我的代码如下:

    <div class="header_text">Brand</div>
    <table style="margin-left: 94px;">
    {foreach from=$interests item=record}
        <tr>
            <td style="width: 150px;">
                <div class="interest_name">
                    <div class="interest_name">
                    {$record.name}
                    </div>
                </div>
            </td>
               {foreach from=$record.type item=type}
                <td style="width: 150px;">

            {* I want to check checkbox after form submit based on Previously selected Form Submission*}      
            <input type="checkbox" name="plan_type[{$record.id}][]" value="{$type}" />
            {$type}

                    <div id="comments">
                        <textarea name="comments[{$record.id}][]" cols="7" rows="2" value="comment"></textarea>
                    </div>
                </td>
                {/foreach}
            <td style="...">
                <input type="checkbox" name="shop" value="Shop" />Shop
                <textarea name="comments[{$record.id}][]" cols="7" rows="2" value="comment"></textarea>
            </td>
        </tr>
    {/foreach}
       </tr>

       <div class="shaukk-buttons universal-red-button home-page-buttons">
        <a herf="#">Add Details</a>
    </table>
    <input type="submit" value="enter" name="submit">

您需要通过php:传递值

global $smarty;
if(isset($_POST["checkboxname"])){
    $smarty->assign("checkboxvalue", $_POST["checkboxname"]);
}

然后在您的模板中:

<input type="checkbox" name="checkboxname" value="{$type}" {if !empty($checkboxvalue) && $checkboxvalue == $type}checked="checked"{/if}/>

我不太确定你想要什么,但你可以只使用代码中的条件来检查输入,并向smarty发送合适的值来触发条件,即:

<input type="checkbox" name="plan_type[{$record.id}][]" value="{$type}" {if $recordcheck}checked="checked"{/if}/>