【问题标题】:keeping history of form data using session使用会话保存表单数据的历史记录
【发布时间】:2018-11-22 23:31:12
【问题描述】:

我有一个表单,用户可以利用它来介绍一些数据来创建徽章。我正在使用会话,以便我可以为用户保留一个小的历史列表,如果他们单击该列表中的一个元素,数据将自动发送到表单。我的问题是,当我单击列表中的一个元素时,会插入一个包含相同数据的新行,并且如果我再次使用列表中已有的相同数据完成表单,它会创建另一行包含相同的数据我已经有一次了。我可以做些什么让我的历史列表只包含唯一值,基本上不会多次出现同一行。 这是我的表单代码:

<form method="get" autocomplete="off">
    <h3>Creaza ecuson</h3>
    <label>
        Nume:<br><input type="text" name="nume" id="nume" required value="<?php echo $search->nume ?>"><br>
        Prenume:<br><input type="text" name="prenume" id="prenume" required value="<?php echo $search->prenume ?>"><br>
        Sex:<br><div class="autocomplete" style="width:300px;">
                                <input id="sex" type="text" name="sex" required value="<?php echo $search->sex ?>">
                     </div><br><br>
        Rol:<br><div class="autocomplete" style="width:300px;">
                                <input id="rol" type="text" name="rol" required value="<?php echo $search->rol ?>">
                     </div><br><br>
        Culoare text:<br><input type="color" name="cul" id="cul" value="<?php echo $search->cul ?>"><br><br>
        Font ecuson:<br><div class="autocomplete" style="width:300px;">
                                <input id="font" type="text" name="font" required value="<?php echo $search->font ?>">
                     </div><br><br>
        Format ecuson (portrait or landscape):<br><div class="autocomplete" style="width:300px;">
                                <input id="format" type="text" name="format" required value="<?php echo $search->format ?>">
                       </div><br><br>
    </label>
    <input type="submit" name="history" value="History" />
    <button type="button" onclick="create()">Creaza</button><br><br>
</form>

我的会话代码:

<?php
        session_start();

        $search = parseRequest();
        storeSearch($search);

        include "form.php";

        $searches = $_SESSION['searches'];


        function storeSearch($search) {
            if (!isset($_SESSION['searches'])) {
                $_SESSION['searches'] = [];
            }

            if (!$search->isEmpty()) {
                $_SESSION['searches'][] = $search;
            }
        }


        function parseRequest() {
            $search = new SearchRequest;
            $search->nume = !empty($_GET['nume']) ? $_GET['nume'] : "";
            $search->prenume = !empty($_GET['prenume']) ? $_GET['prenume'] : "";
            $search->sex = !empty($_GET['sex']) ? $_GET['sex'] : "";
            $search->rol = !empty($_GET['rol']) ? $_GET['rol'] : "";
            $search->cul = !empty($_GET['cul']) ? $_GET['cul'] : "";
            $search->font = !empty($_GET['font']) ? $_GET['font'] : "";
            $search->format = !empty($_GET['format']) ? $_GET['format'] : "";
            return $search;
        }

        /**
         * search request
         */
        class SearchRequest
        {
            public $nume = "";
            public $prenume = "";
            public $sex = "";
            public $rol = "";
            public $cul = "";
            public $font = "";
            public $format = "";

            function toQueryString() {
                $params = [
                        'nume' => $this->nume,
                        'prenume' => $this->prenume,
                        'sex' => $this->sex,
                        'rol'=> $this->rol,
                        'cul'=> $this->cul,
                        'font'=> $this->font,
                        'format'=> $this->format
                ];

                return http_build_query($params);
            }

            function isEmpty() {
                return !$this->nume || !$this->prenume || !$this->sex || !$this->rol || !$this->cul || !$this->font || !$this->format;
            }
        }

        ?>

还有所谓的历史代码:

<?php
            foreach ($searches as $s) {
            ?>
             <li><a href="creare.php?<?php echo $s->toQueryString() ?>">
                    <?php echo $s->nume?> - <?php echo $s->prenume?> - <?php echo $s->sex?> - <?php echo $s->rol?> - <?php echo $s->cul?> - <?php echo $s->font?> - <?php echo $s->format?>
                  </a></li>
            <?php
            }
            ?>

对于我提出的问题,我认为不需要在此处发布具有自动完成功能的脚本。如果需要我会提供。

【问题讨论】:

  • 您对唯一数据的定义是什么?您是在 email 之类的字段上命名它还是在 IP Address 之类的其他字段上命名它?
  • 如果你在图片中看到我的历史列表中有 6 次相同的行,我认为这更好地解释 drive.google.com/file/d/10kWMpouY8QfjC5e49HYGNBwk_Lw2HXVW/view
  • 我只想要那行 1 次
  • 一台电脑可以注册/介绍多少人?
  • 每台电脑只限一人

标签: php html session session-cookies


【解决方案1】:

基于 CFP Support 的回答,这里有一种稍微不同的方法来创建表单和处理程序。它与您的非常相似,但我的逻辑结构有所不同。我只从您的表单中添加了 3 个字段,但您可以轻松添加其余字段。

小提琴 - http://phpfiddle.org/lite/code/354t-6sgn

<?php

session_start();

// Initialize the cart if it needs it.
if (!isset($_SESSION['cart'])) {
    $_SESSION['cart'] = [];
}

// Should we show cart?
$showCart = isset($_GET['cart']) && $_GET['cart'] === 'true';

// Should we clear the cart?
if (isset($_GET['clear']) && $_GET['clear'] === 'true') {
    $_SESSION['cart'] = [];
}

// Grab the current cart.
$cart = $_SESSION['cart'];

// The form was submitted
if (isset($_POST['submit'])) {

    // Copy the POST data into a variable so we can modify it without side effects.
    $formData = $_POST;

    // Remove the submit button from the form data
    unset($formData['submit']);

    // Check if it is in the cart already.
    if (!in_array($formData, $cart)) {
        // If not, then add it.
        $cart[] = $formData;
    }

    // Store the cart in the session.
    $_SESSION['cart'] = $cart;
}
?>


<html>
<head>
    <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet"
          integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous">
</head>

<body>
<div class="container">
    <form method="post" autocomplete="off">
        <h3>Creaza ecuson</h3>
        <div class="mb-3">
            <div class="col-md-6 mb-3">
                <label for="nume">Nume:
                    <input type="text" name="nume" id="nume" class="form-control" required>
                </label>
            </div>
        </div>


        <div class="mb-3">
            <div class="col-md-6 mb-3">
                <label for="prenume">Prenume:
                    <input type="text" name="prenume" id="prenume" class="form-control" required>
                </label>
            </div>
        </div>


        <div class="mb-3">
            <div class="col-md-6 mb-3">
                <label for="sex">Sex:
                    <input type="text" name="sex" id="sex" class="form-control" required>
                </label>
            </div>
        </div>

        <button class="btn btn-primary" name="submit" type="submit">Create</button>

        <?php
        // Toggle show/hide history
        if ($showCart) { ?>
            <a class="btn btn-primary" href="?" role="button">Hide Cart</a>
        <?php } else { ?>
            <a class="btn btn-primary" href="?cart=true" role="button">Show Cart</a>
        <?php }

        // If the cart is not empty, allow user to clear it.
        if (!empty($cart)) { ?>
            <a class="btn btn-primary" href="?clear=true" role="button">Clear Cart</a>
        <?php }
        ?>

    </form>

    <?php
    // Show the cart.
    if ($showCart) {
        echo '<pre>';
        var_dump($cart);
        echo '</pre>';
    }
    ?>
</div>
</body>
</html>

【讨论】:

    【解决方案2】:

    这是一种方法和少量伪代码,您可以用您的代码库实现类似的东西。

    这个想法是,由于只有一个人可以在一台计算机上注册,因此在会话中为该人存储一个唯一 ID。然后在将数据输入会话时,检查该 ID 是否存在。

    如果存在,则不添加,如果不存在,则添加。

    伪代码

    $uniqueID = hash("sha256", $_SERVER['REMOTE_ADDR']); //generate a unique ID depending on IP since that would be unique for each computer
    
    //insert into your session
    if(!in($sessionHandler, $uniqueid)
    {
        //insert now
    }
    

    【讨论】:

      【解决方案3】:

      也许就这么简单

      function storeSearch($search) {
              if (!isset($_SESSION['searches'])) {
                  $_SESSION['searches'] = [];
              }
      
              if (!$search->isEmpty() && !in_array($search,$_SESSION['searches') {
                  $_SESSION['searches'][] = $search;
              }
          }
      

      【讨论】:

      • 它有效..它是如此简单以至于我什至没有想到我正在考虑更复杂的事情
      • 很高兴它有帮助 :) 有时我们看不到所有树木的森林 - 我去过那里很多次(有时“太多”!)。祝你的项目好运。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-02-20
      • 1970-01-01
      • 2020-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多