如何在页面提交和重新加载后保留复选框状态?

时间:2017-12-30 16:05:52

标签: php mysql

我有一个名为property_amenities的表,其中包含列; amenity_idamenity_name,其值在表单上填充为复选框。表单提交后,选中的值将作为数组插入列property_listings的{​​{1}}表中。

property_amenities

上面的代码工作正常。唯一的问题是,当我提交表单并且页面重新加载 <?php $db_host="localhost"; $db_name="cl43-realv3"; $db_user="root"; $db_pass=""; try { $DB_con = new PDO("mysql:host={$db_host};dbname={$db_name}",$db_user,$db_pass); $DB_con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch(PDOException $e) { $e->getMessage(); } if (isset($_POST['list_property'])) { if (isset($_POST['property_amenities'])) { $property_amenities = implode(",",$_POST['property_amenities']); }else{ $property_amenities = "";} } ?> <!DOCTYPE html> <html> <head> <title></title> </head> <body> <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post"> <div class="row clearfix"> <div class="col-lg-12 col-md-6 col-sm-12"> <?php $stm = $DB_con->prepare("SELECT * FROM property_amenities"); $stm->execute(array( )); while ($row = $stm->fetch()){ $amenity_id = $row['amenity_id']; $amenity_name = $row['amenity_name']; $List[] ="<label class=checkbox> <input type=checkbox name='property_amenities[]' value='$amenity_name'> $amenity_name</label>"; } $allamenities = $stm->rowCount(); $how_many_chunks = $allamenities/3; $roster_chunks = array_chunk($List, round($how_many_chunks), true); ?> <label>Property Amenities</label></small> <?php foreach ($roster_chunks as $roster_chunk_key => $roster_chunk_value) { echo "<div class='col-lg-3 col-md-6 col-sm-12'>"; echo join($roster_chunk_value); echo '</div>'; } ?> </div> </div><!-- end row --> <button type="submit" class="btn btn-primary" name="list_property" >SUBMIT PROPERTY</button> </form> </body> </html> 的所有复选框都未选中时,我想要的是在页面提交之前检查的复选框。

我怎样才能做到这一点?

这是property_amenities表

property_amenities

2 个答案:

答案 0 :(得分:2)

只需在您的数据库中检查选中了哪些复选框,并将<div class="outer"> <div class="inner">Click me</div> </div>属性包含在您回复复选框时的那些属性中:

checked

修改

(回应comment

由于每个$checked = (condition) ? "checked" : ""; $List[] = "<label class=checkbox> <input type=checkbox name='...' value='$amenity_name' $checked/> $amenity_name </label>"; 的{​​{1}}是便利设施的名称,因此您可以使用提交表单时获得的value数组轻松找到复选框:

  1. 首先,为已检查的便利设施(checkbox)创建一个空数组。
  2. 然后,使用$_POST['property_amenities']检查是否有任何经过检查的便利设施。如果是,请更新上面创建的数组的值。
  3. $amenities_checked循环中,检查该虚拟设施的名称是否在该数组中。
  4. 步骤1&amp;的代码2:

    isset($_POST["property_amenities"])

    第3步的代码:

    while

    完整代码:

    (该代码段用于折叠代码)

    # Initialise an array to store the amenities checked.
    $amenities_checked = [];
    
    # Check whether the form has been submitted.
    if (isset($_POST["list_property"])) {
        # Initialise the amenities string.
        $property_amenities = "";
    
        # Check whether any checkbox has been checked.
        if (isset($_POST["property_amenities"])) {
            # Update the checked amenities array.
            $amenities_checked = $_POST["property_amenities"];
    
            # Implode the array into a comma-separated string.
            $property_amenities = implode(",", $amenities_checked);
        }
    }
    

答案 1 :(得分:0)

首先,您从property_listings表中获取数据,并使用explode函数将property_amenities字符串转换为数组。然后你可以使用in_array()函数来检查你的字符串。如果数组中存在amenity_name,则可以使用checked属性。

例如

$stm = $DB_con->prepare("SELECT * FROM property_amenities");
$stm->execute(array());

// fetch data from property_amenities then use explode function
$property_amenities = explode(",",$data);

      while ($row = $stm->fetch()){
        $amenity_id = $row['amenity_id'];
        $amenity_name = $row['amenity_name']; 
        $List[] ="<label class=checkbox> <input type=checkbox name='property_amenities[]' value='$amenity_name' <?php if(in_array($amenity_name,$property_amenities)){ echo "checked=true"; }?>> $amenity_name</label>";
      }