如何保存为每个用户帐户勾选的复选框?

时间:2017-12-24 02:34:19

标签: php

我是一名非常初学的程序员并正在执行一个项目,目前我有多个复选框,您可以在创建帐户后进行检查。遗憾的是,该帐户并不知道如何记住用户在其帐户上检查的框。所以问题很简单:我如何做到这一点,以便用户的选择被注册到他的账户?

这是index.php:

std::any

这是server.php:

<?php 
session_start(); 

if (!isset($_SESSION['username'])) {
  $_SESSION['msg'] = "You must log in first";
  header('location: login.php');
}
if (isset($_GET['logout'])) {
session_destroy();
unset($_SESSION['username']);
header("location: login.php");
}
?>


<!DOCTYPE html>
<html>
    <head> 
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel= "stylesheet" type= "text/css" href= "stylehome.css">
    <title>FFBE Unit Collection</title>
</head>


<body>

<header>
    <h1>FFBE Unit Collection</h1>

</header>

<div class="content">
<!-- notification message -->
<?php if (isset($_SESSION['success'])) : ?>
  <div class="error success" >
    <h3>
      <?php 
        echo $_SESSION['success']; 
        unset($_SESSION['success']);
      ?>
    </h3>
  </div>
<?php endif ?>

<!-- logged in user information -->
<?php  if (isset($_SESSION['username'])) : ?>
    <p>Welcome <strong><?php echo $_SESSION['username']; ?></strong></p>
    <p> <a href="index.php?logout='1'" style="color: red;">Logout</a>       </p>
<?php endif ?>
</div>

<br><br>

     <!--Here starts the 5 star bases-->

    <img src="ressources/fivestar.png" class="star"/>
    <br>
    <div id="rainbow">

    <input type="checkbox" name="units" id="lightning">
    <label for="lightning">
    <img src="units/lightning.png"/>  
    </label>

    <input type="checkbox" name="units" id="delita">
    <label for="delita">
    <img src="units/delita.png"/>  
    </label>

    <input type="checkbox" name="units" id="ramza">
    <label for="ramza">
    <img src="units/ramza.png"/>  
    </label>

    <input type="checkbox" name="units" id="darkknightcecil">
    <label for="darkknightcecil">
    <img src="units/darkknightcecil.png"/>  
    </label>

    <input type="checkbox" name="units" id="luneth">
    <label for="luneth">
    <img src="units/luneth.png"/>  
    </label>

    <input type="checkbox" name="units" id="gilgamesh">
    <label for="gilgamesh">
    <img src="units/gilgamesh.png"/>  
    </label>

    <input type="checkbox" name="units" id="noctis">
    <label for="noctis">
    <img src="units/noctis.png"/>  
    </label>

    <input type="checkbox" name="units" id="darkfina">
    <label for="darkfina">
    <img src="units/darkfina.png"/>  
    </label>



    </div>





<br/>
<footer>





</footer>

2 个答案:

答案 0 :(得分:0)

我假设,您的项目有一个数据库来存储注册用户信息。如果是,则在数据库中创建一个表。在该表上,为用户可以检查的每个选项创建一列,单独使用用户ID列作为外键。然后为该表的每个列设置一个值(通常为1),以记住该表的哪些选项。

答案 1 :(得分:0)

Checkboxes而不是radio buttons使用不同的names,除非它们属于checkbox group,在这种情况下,它们与[]具有相同的名称像这样结束name="chkbx1[]"。接收PHP文件然后查询checkbox的名称,以查看它是否与$_POST$_GET变量一起存在。如果是,则表示它是checked。如果不是,则表示它是unchecked

id标记与PHP无关。它仅用于client side编程目的。对于sever side(在这种情况下为PHP),请参阅name标记以进行身份​​识别。

这是来自<div> rainbow的代码:

注意:我更改了所有复选框名称以匹配其ID

<form action="pathToYourPHPfile/example.php" method="POST">
  <table cellspacing="15">
  <tr>
    <td>
      <label for="lightning"><img src="units/lightning.png"/></label>
      <input type="checkbox" name="lightning" id="lightning">
    </td>
    <td>
      <label for="delita"><img src="units/delita.png"/></label>
      <input type="checkbox" name="delita" id="delita">
    </td>
    <td>
      <label for="ramza"><img src="units/ramza.png"/></label>
      <input type="checkbox" name="ramza" id="ramza">
    </td>
    <td>
      <label for="darkknightcecil"><img src="units/darkknightcecil.png"/></label>
      <input type="checkbox" name="darkknightcecil" id="darkknightcecil">
    </td>
    <td>
      <label for="luneth"><img src="units/luneth.png"/></label>
      <input type="checkbox" name="luneth" id="luneth">
    </td>
    <td>
      <label for="gilgamesh"><img src="units/gilgamesh.png"/></label>
      <input type="checkbox" name="gilgamesh" id="gilgamesh">
    </td>
    <td>
      <label for="noctis"><img src="units/noctis.png"/></label>
      <input type="checkbox" name="noctis" id="noctis">
    </td>
    <td>   
      <label for="darkfina"><img src="units/darkfina.png"/></label>
      <input type="checkbox" name="darkfina" id="darkfina">
    </td>
   <tr>
  </table>

  <input type="submit" value="click Me To Submit">
</form>

这将是一个有效的查询,以查看是否已使用接收PHP文件example.php检查复选框:

<?php
    // an array of possible checkbox names
    $myCheckBoxArray = array("lightning", "delita", "ramza", "darkknightcecil", "luneth", "gilgamesh", "noctis", "darkfina");

    // connect to the database
    $db = mysqli_connect('localhost', ...  // database connection 

    // go through the list of checkbox names and check if checked
    for($i=0; $i<count($myCheckBoxArray); $i++)
    {
        // if its set that means it was checked. An example is isset($_POST['delita'])
        if( isset( $_POST[$myCheckBoxArray[$i]] ) )
        {
            echo $myCheckBoxArray[$i].' is set<br/>';

            // write to database
            mysqli_query($db, "INSERT INTO tableName (user, chose) VALUES ('batman', '".$myCheckBoxArray[$i]."')");
        }
        else
        {
            echo $myCheckBoxArray[$i].' is not set<br/>';
            // Do something
        }
    }
?>

在MySQL中创建一个示例表,如下所示:

create table `tableName`
(
   `id` INT NOT NULL AUTO_INCREMENT,
   `user` VARCHAR(150) NOT NULL,
   `chose` VARCHAR(70) NOT NULL,
   PRIMARY KEY(`id`)
) ENGINE=InnoDB;