【问题标题】:(PHP) How to set default value to unchecked checkbox(PHP) 如何将默认值设置为未选中的复选框
【发布时间】:2016-09-19 05:36:53
【问题描述】:

我是 PHP 新手。

现在我遇到了关于复选框值的问题。我从这些页面中搜索他们的解决方案不适用于我的情况的答案。

Trying to set default checkbox value if not checked

Post the checkboxes that are unchecked(不知道JS放哪了)

how do I get all checkbox variables even if not checked from HTML to PHP?

到目前为止,这些是我的代码...

<form method="POST" action="testinsertskill.php">

<br><br>Required Skills : 

**//skip the sql parts because there is no problem from calling the data from database**

<?php
while ($row2 = mysql_fetch_array($result2, MYSQL_ASSOC)) {

?>
<input type="hidden" id="hidden_skills" name="skills[]" value="0" />
<br /><input type="checkbox" id="skills" name="skills[]" value="1" /><?php echo $row2['Skill_Name']; ?><br />

<?php
$count++;
}
?>
<input type="hidden" name="counter" value="<?=$count?>" />

<input type="submit" name="submit" value="Confirm">

</form>

这是 testinsertskill.php 页面

<?php session_start();

        $servername = "localhost";
        $username = "root";
        $password = "rootroot";
        $dbname = "onlinerecruitment";

        // Create connection
        $conn = new mysqli($servername, $username, $password, $dbname);
        // Check connection
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        } 


$skill = $_POST['skills'];
$count = $_POST['counter'];


for($i = 0; $i < $count; $i++){

    $sql = "INSERT INTO applicant_skill VALUES('".$skill[$i]."')"; 

        $resultt = "";

        if ($conn->query($sql) == TRUE) {
           $resultt = "FINISH";
        } else {
           $resultt = "ERROR";
        }

}

$conn->close();

?>

假设有 5 个技能可供选择,我选择第 2 和第 4 个。

数据库中的预期结果应该是 (0,1,0,1,0),但由于隐藏输入,结果是 (0,0,1,0,0)。现在我不知道该怎么做,因为我提到的那些链接无法解决我的问题,或者我不知道我必须把它放在哪里。请帮忙。

【问题讨论】:

    标签: javascript php html checkbox


    【解决方案1】:

    我假设你也有一些 Skill_Id 那么代码可能看起来像这样

    <form method="POST" action="testinsertskill.php">
    
    <br><br>Required Skills : 
    
    **//skip the sql parts because there is no problem from calling the data from database**
    
    <?php
    while ($row2 = mysql_fetch_array($result2, MYSQL_ASSOC)) {
    
    ?>
    <input type="hidden" name="skills[<?= $row2['Skill_Id'] ?>]" value="0" />
    <br /><input type="checkbox" name="skills[<?= $row2['Skill_Id'] ?>]" value="1" /><?= $row2['Skill_Name'] ?><br />
    
    <?php
    }
    ?>
    
    <input type="submit" name="submit" value="Confirm">
    
    </form>
    

    然后插入代码:

    //....
    foreach($_POST['skills'] as $skillId => $skill){
    
        $sql = "INSERT INTO applicant_skill VALUES('".$skill."')"; 
    
            $resultt = "";
    
            if ($conn->query($sql) == TRUE) {
               $resultt = "FINISH";
            } else {
               $resultt = "ERROR";
            }
    
    }
    

    【讨论】:

    • 我有继续的问题。如果我也想插入 Skill_ID 的值怎么办?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-11
    • 2021-04-26
    • 1970-01-01
    • 1970-01-01
    • 2016-06-22
    • 2015-11-17
    相关资源
    最近更新 更多