在表单上提交预先复选的复选框提交PHP MYSQL

时间:2018-03-17 17:56:52

标签: php mysql

如何在提交时提交预先选中的复选框?

现在我有

$checked= 'checked="checked"';
<input type="checkbox" name="entity_id['.$row['entity_id'].']" value="Yes" '.$checked.'>

在提交时,只有在手动选中/取消选中时才会发布。

我的$ checked实际上来自for-each mysql查询。

感谢任何帮助。

更新

    <form method="post">';
if($hass_lights == 'Yes'){$user->getLights($userid);}
  exit('

<br><b>Google is requiring access to your basic profile information.</b><br><br>
<input type="submit" class="btn btn-text" style="height:40px; width:100px" name="authorized" value="Allow" /> <input type="submit" class="btn btn-text" style="height:40px; width:100px" name="authorized" value="Deny" />
</form>
');

     function getLights($userID){
                $stmt = $this->db->prepare("SELECT * FROM hass_entities WHERE id = :id AND devicetype= 'light' ORDER BY friendly_name ASC");
                $stmt->bindParam(':id',$userID);    
                $stmt->execute();
            $userData = $stmt->fetchAll();
            echo '
            <div class="container">
  <button type="button" class="btn btn-info" data-toggle="collapse" style="width:120px" data-target="#lights">Lights</button>
  <br/><div id="lights" class="collapse"><br/><table border=0>';
            foreach( $userData as $row ) {
if($row['enabled'] == 'Yes'){
$checked = 'checked="checked"'; 
}else{  
$checked = '';       
}
    echo '<tr><td><label id="'.$row['entity_id'].'">'.$row['friendly_name'].' </label></td><td><input type="checkbox" name="entity_id['.$row['entity_id'].']" value="Yes" '.$checked.'></td></tr>'; 
        }
            echo '</table></div></div><br/>';
    }

丹尼斯

2 个答案:

答案 0 :(得分:0)

试试这个:

<input type="checkbox" name="entity_id['.$row['entity_id'].']" value="Yes" checked>

答案 1 :(得分:0)

你的输入字段是用echo语句写的吗?很高兴看到整个表单,但这是你的代码工作:

<form method=POST>
<?php
var_dump($_POST);
$row = [];
$row['entity_id'] = 1;
$checked= 'checked="checked"';
echo '<input type="checkbox" name="entity_id['.$row['entity_id'].']" value="Yes" '.$checked.'>';
?>
<button type="submit">submit</button>

</form>