【问题标题】:Checkboxes not saving when unchecked POST PHP [duplicate]未选中 POST PHP 时复选框不保存 [重复]
【发布时间】:2017-11-12 16:55:01
【问题描述】:

我知道 Mysqli 已解密,但服务器不允许我在 Mysqli 下划线,但目前这不是我的问题,我的问题是,如果我的复选框未选中,它不会更新到 0 其中从 if then 声明 else。

<?php 
@mysql_connect("localhost", "xxx", "xxx") or die(mysql_error());
@mysql_select_db("xxx") or die(mysql_error());
@mysql_query("SET NAMES utf8");        error_reporting(E_ALL);
ini_set('display_errors', 1);





$chk1 = $_POST['chk1'];

foreach($chk1 as $i=>$s){
    if($s == TRUE)
    {
        echo $s, '<br/>';
        $sql = mysql_query("UPDATE `xxx`.`cars` SET `show` = '1' WHERE cars_id='".$s."'");
    } else {    
        echo $s, '<br/>';
        $sql = mysql_query("UPDATE `xxx`.`cars` SET `show` = '0' WHERE cars_id='".$s."'");
    }
    }

?>

*添加

<form action="./pages/checkbox.php" method="post">
<section class="content" class="table details">
    <table class="dashboard">
        <tr class="top">
            <td width="40">#</td>
            <td width="40"Show</td>
            <td width="40">Photo</td>
            <td width="80%"Name</td>
        </tr>
    <tbody id="the-list">
<?
$result= mysql_query("SELECT * FROM cars");
$counter = 1;
?>
<?php
 while($row = mysql_fetch_array($result)){
        echo '<tr>
            <td>' . $counter++ . '</td>
            <td><input value="'.$row['cars_id'].'" id="chk1" name="chk1[]" type="checkbox"';
            if ($row['show'] == true){  
                echo 'checked></td>'; 
                }else 
                { 
                echo 'unchecked></td>'; 
            }
        echo '<td><img src="../xml/images/'.$row['cars_id'].'-1.JPG"  width="120px"></td>';     
        echo "<td><h3> ", $row['brand']," " . $row['model'], " " . $row['type'], " &euro; " . $row[cars_id'], " </h3></td>";
        echo '</tr>';
 }
?> <input type="submit" name="Submit" value="Safe">
    </form> 

在“重复”帖子中,我没有找到如何将其放入 while 循环中,请描述这是同一个问题。

【问题讨论】:

  • 不,因为我在那篇文章中没有看到在我的表单中隐藏了一段时间。 - 循环
  • 如果您在不那么重复的帖子中找不到清晰的遮阳篷,这会让人们有点害怕提问,因为我真的没有看到我正在寻找的问题。

标签: php html mysql


【解决方案1】:

如果未选中复选框,则不会发布。所以 ID 不在你的数组中。

检查你的帖子数据,你会发现复选框不见了。


取消选中复选框的最佳方法是覆盖它们。

<form method="post">
  <input type="hidden" name="checkbox" value="0">
  <input type="checkbox" name="checkbox" value="1">
  <button type="submit">Check result</button>
</form>

【讨论】:

  • 也许这个问题不太聪明,但是我也在学习。如何查看我的发布数据?
  • 谢谢,(抱歉想编辑我的帖子必须习惯stackoverflow的界面)。但是,如果您看到我的帖子已编辑,我的表单在哪里,如果值是自动的,我应该在哪里放置隐藏输入,我不明白这是如何工作的。
  • 虽然您的总体答案(未选中时根本不会发送复选框)是正确的,但发送带有表单的复选框列表并不是最佳解决方案。您必须做额外的工作(又名爆炸),并且您依赖客户端获取您不应该提供的信息。如果有人更改了ids 输入的内容会怎样?遵循相同的概念,但不要从表单中获取允许的复选框列表。您可以在 PHP 脚本中轻松地对该列表进行硬编码。
  • @ConorMancone 你能详细解释一下吗?谢谢,现在我把它放了,但仍然没有在 checkbox.php 中更新,所有这些都保存为 1 而不是 0
  • 另一种解决方案是通过渲染将 ID 保存为会话。明天我可以通过会话更新我的答案。所以客户端不能操作 id。
【解决方案2】:

复选框仅在选中时发布数据。 检查它是否发布了

    <?php
    if(isset($_POST["chk1"])){
    // Run your sql code in here to avoid unwanted output
// Set your check variable
$chk1=$_POST["chk1"];
// Then your loop
foreach($chk1 as $i=>$s){
    if($s == TRUE)
    {
        echo $s, '<br/>';
        $sql = mysql_query("UPDATE `xxx`.`cars` SET `show` = '1' WHERE cars_id='".$s."'");
    } else {    
        echo $s, '<br/>';
        $sql = mysql_query("UPDATE `xxx`.`cars` SET `show` = '0' WHERE cars_id='".$s."'");
    }
    }
    }
    ?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-21
    • 2021-01-05
    • 2016-08-24
    • 2020-03-11
    • 1970-01-01
    • 1970-01-01
    • 2010-12-21
    相关资源
    最近更新 更多