dongshi3605 2013-05-20 10:19
浏览 21
已采纳

记住表单复选框值

I want a form to remember the user's input whenever an error has occurred. I got this to work using for the text input using:

echo "<input id=\"quote_square\" type=\"text\" name=\"square\" placeholder=\"Square\" value=\"".  $_POST['square'] . "\">";

For checkboxes I can't get this to work. I did find this example of how this can be done:

   <input type="checkbox" name="subscribe" <?php echo (isset($_POST['opdracht'])?'checked="checked"':'') ?> />

But I'm echoing my entire form into php and saving the checkbox input into an array:

echo "<input id=\"quote_round\" type=\"text\" name=\"round\" placeholder=\"Round\" value=\"".  $_POST['round'] . "\">";
echo "<input id=\"quote_square\" type=\"text\" name=\"square\" placeholder=\"Square\" value=\"".  $_POST['square'] . "\">";
echo "<label class=\"checkbox\"><input type=\"checkbox\" name=\"opdracht[]\" value=\"Blue\">Blue</label>";
echo "<label class=\"checkbox\"><input type=\"checkbox\" name=\"opdracht[]\" value=\"Black\">Black</label>";
echo "<label class=\"checkbox\"><input type=\"checkbox\" name=\"opdracht[]\" value=\"Red\">Red</label>";
echo "<label class=\"checkbox\"><input type=\"checkbox\" name=\"opdracht[]\" value=\"White\">White</label>";

Can anybody give a suggestion how I can get the checkboxes to save the user's input?

  • 写回答

5条回答 默认 最新

  • duanqiongniu1469 2013-05-20 10:34
    关注

    Try

    echo "<label class=\"checkbox\"><input type=\"checkbox\" ", 
        in_array("Blue", $_POST["opdracht"])?"checked='checked' ":"", 
        "name=\"opdracht[]\" value=\"Blue\">Blue</label>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部