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>";
    
    打赏 评论
  • dsegw3424 2013-05-20 18:27
       echo '<input type="checkbox" name="subscribe"' .($yourData['opdracht']?'checked="checked"':'').' />';
    
    打赏 评论
  • douwen1549 2013-05-20 18:34

    Try this:

    echo isset($yourData['opdracht']) ? 'checked="checked"' : '';
    

    Like

     <input type="checkbox" name="subscribe" <?php  echo isset($yourData['opdracht']) ? 'checked="checked"' : ''; ?> />
    
    打赏 评论
  • dpnvrt3119 2013-05-20 18:35

    I don't believe you're using the array correctly. This kind of array referencing is used where you have multiple versions of the same data being submitted. The array keys get filled in when the form is submitted, and values that are not checked will not be indexed, so you'll end up with different sized arrays, depending on how many boxes have been checked. Telling your values apart will then be problematic. It needs to be something like this;

    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=\"opdrachtBlue\" value=\"Blue\">Blue</label>";
    echo "<label class=\"checkbox\"><input type=\"checkbox\" name=\"opdrachtBlack\" value=\"Black\">Black</label>";
    echo "<label class=\"checkbox\"><input type=\"checkbox\" name=\"opdrachtRed\" value=\"Red\">Red</label>";
    echo "<label class=\"checkbox\"><input type=\"checkbox\" name=\"opdrachtWhite\" value=\"White\">White</label>";
    

    You can then use the

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

    to get if the checkbox has been previously selected.

    打赏 评论
  • doulidai6316 2013-05-20 18:41

    Try this

    <label class="checkbox"><input type="checkbox" checked= "<?php in_array('Blue', $deep['opdracht'])?'checked' :''; ?>" value="Blue">Blue</label>
    
    打赏 评论

相关推荐 更多相似问题