dongshi3605 2013-05-20 18: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 18:34
    关注

    Try

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

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料