Just Me
Just Me

Reputation: 65

keeping history of form data using session

I have a form that users can utilize to introduce some data to create a badge. I'm using session so that i can keep like a little history list for the users, and also if they click on one element from that list the data will be sent to the form automatically. My problem is that when i click on one element from the list a new row is inserted containing the same data, and also if i complete the form with identical data that i already have in my list again it creates another line containing the same data that i already have once. Can i do something so that my history list to contain only unique values, basically to not have the same line multiple times. This is my code for the form:

<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>

My session code:

<?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;
            }
        }

        ?>

And the so called history code:

<?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
            }
            ?>

I don't think the script with the autocomplete function needs to be posted here for the question that i asked. If needed i will provide.

Upvotes: 1

Views: 819

Answers (3)

waterloomatt
waterloomatt

Reputation: 3742

Building on CFP Support's answer, here's a slightly different approach to how I would create the form and handler. It's very similar to yours but I structured the logic a bit differently. I only added 3 fields from your form but you can easily add the remaining fields.

Fiddle - 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>

Upvotes: 1

Akshay
Akshay

Reputation: 2229

Here's a way and little pseudo-code, you could implement something similar with your codebase.

The idea is, since from one computer only one person can sign up, store a unique ID for that person in session. Then when entering the data into session, check if that ID is present or not.

If it's present, do not add, if it's not, add.

Pseudo-code

$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
}

Upvotes: 0

Apps-n-Add-Ons
Apps-n-Add-Ons

Reputation: 2146

perhaps something as simple as

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

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

Upvotes: 3

Related Questions