1

I have a form from which i take some datas and using session i want to keep that datas as a history on my page, and also when i click on one line from my history i want my datas to be autocomplete in my form. I saw an example on one page and i tried doing it to apply to what i want but it's not quite functional.

This is my code for the form:

<form method="get">
         Create:<br><br>

                  Name:<br> <input type="text" id="name" value="" /><br>
                  surname:<br> <input type="text" id="surname" value="" /><br>
                  Sex:<br> <div class="select-wrapper">
                              <select name="sex" id="sex">
                                 <option value="">- Sex -</option>
                                 <option value="woman">Woman</option>
                                     <option value="male">Male</option>
                              </select>
                             </div>
                  Role: <div class="select-wrapper">
                              <select name="rol" id="rol">
                                 <option value="">- Role -</option>
                                 <option value="visitor">Visitor</option>
                                 <option value="Professor">Professor</option>
                                 <option value="Student">Student</option>
                              </select>
                             </div>
                   Text color: <div class="select-wrapper">
                              <select name="cul" id="cul">
                                 <option value="">- Text color -</option>
                                 <option value="red">Red</option>
                                 <option value="blue">Blue</option>
                                 <option value="black">Black</option>
                              </select>
                             </div>
                   Font text: <div class="select-wrapper">
                              <select name="font" id="font">
                                 <option value="">- Font text -</option>
                                 <option value="15px Arial">Arial</option>
                                 <option value="15px Times New Roman">Times New Roman</option>
                                 <option value="15px Georgia">Georgia</option>
                                 <option value="15px Comic Sans MS">Comic Sans MS</option>
                                 <option value="15px Lucida Sans Unicode">Lucida Sans Unicode</option>
                                 <option value="15px Courier New">Courier New</option>
                              </select>
                             </div>
                   Format : <div class="select-wrapper">
                                    <select name="format" id="format">
                                       <option value="">- Format -</option>
                                       <option value="portrait">Portrait</option>
                                       <option value="landscape">Landscape</option>
                                    </select>
                                  </div>
                   Style text: <div class="select-wrapper">
                                    <select name="stil" id="stil">
                                       <option value="">- Style text -</option>
                                       <option value="stil1">Stil1</option>
                                       <option value="stil2">Stil2</option>
                                       <option value="stil3">Stil3</option>
                                       <option value="stil4">Stil4</option>
                                       <option value="stil5">Stil5</option>
                                       <option value="stil6">Stil6</option>
                                    </select>
                                  </div>
</form>

And this is where i tried to use session:

<?php
session_start();

$createpas = parseRequest();
storecreatepas($createpas);

include "form.php";

$createpases = $_SESSION['createpases'];
include "history.php";




function storecreatepas($createpas) {
    if (!isset($_SESSION['createpases'])) {
        $_SESSION['createpases'] = [];
    }

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


function parseRequest() {
    $createpas = new createpasRequest;
    $createpas->cul = !empty($_GET['cul']) ? $_GET['cul'] : "";
    $createpas->font = !empty($_GET['font']) ? $_GET['font'] : "";
    $createpas->format = !empty($_GET['format']) ? $_GET['format'] : "";
    $createpas->stil = !empty($_GET['stil']) ? $_GET['stil'] : "";
    return $createpas;
}


/**
 * createpas request
 */
class createpasRequest
{
    public $cul = "";
    public $font = "";
    public $format = "";
    public $stil = "";

    function toQueryString() {
        $params = [
                'cul' => $this->cul,
                'font' => $this->font,
                'format' => $this->format,
                'stil' => $this->stil
        ];

        return http_build_query($params);
    }

    function isEmpty() {
        return !$this->cul || !$this->font || !$this->format || !$this->stil;
    }

    function culAsObject() {
        return new DateTime($this->cul);
    }

    function fontAsObject() {
        return new DateTime($this->font);
    }
    function formatAsObject() {
        return new DateTime($this->format);
    }
    function stilAsObject() {
        return new DateTime($this->stil);
    }

}

And the display code:

<ul>
    <?php
    foreach ($createpases as $s) {
    ?>
     <li><a href="search.php?<?php echo $s->toQueryString() ?>">
            <?php echo $s->cul?> - <?php echo $s->font?> - <?php echo $s->format?> - <?php echo $s->stil?>
          </a></li>
    <?php
    }
    ?>
</ul>

It works just fine until some point. It gets my datas from my form posts them on the page but when i click on them they don't autocomplete in in my form. And also if i want the options to be unique in the list how can i do that? Right now if i complete the same datas 2 or 3 times they appear multiple times in my list. Thank you for your help!

Just Me
  • 65
  • 10
  • Your HTML form does not use any data from your `$_SESSION` so how will it know what "should" be in the form inputs? – Martin Jun 04 '18 at 15:44
  • The form that i saw was like this but i don't know how to adapt it for wnat i need: `

    Dream trip

    `
    – Just Me Jun 04 '18 at 15:48

2 Answers2

1

You need to start the session in the Form page and then check if the SESSION array contains the values to be echoed.

So your code will become:

<?php session_start(); 
 if(!isset($_SESSION['name']){$_SESSION['name']=''}
 if(!isset($_SESSION['sex']){$_SESSION['sex']=''}
 if(!isset($_SESSION['rol']){$_SESSION['rol']=''}
?>
//this way you will not have issues on page first load before the user fills in the form
<form method="get">
         Create:<br><br>

                  Name:<br> <input type="text" id="name" value="<?php echo $_SESSION['name']; ?>" /><br>
                  surname:<br> <input type="text" id="surname" value="" /><br>
                  Sex:<br> <div class="select-wrapper">
                              <select name="sex" id="sex">
                                 <option value="" <?php if($_SESSION['rol']==''){echo 'selected'} ?>>- Sex -</option>
                                 <option value="woman" <?php if($_SESSION['rol']=='woman'){echo 'selected'} ?>>Woman</option>
                                     <option value="male" <?php if($_SESSION['rol']=='visitor'){echo 'selected'} ?>>Male</option>
                              </select>
                             </div>
                  Role: <div class="select-wrapper">
                              <select name="rol" id="rol">
                                 <option value="" <?php if($_SESSION['rol']==''){echo 'selected'} ?>>- Role -</option>
                                 <option value="visitor" <?php if($_SESSION['rol']=='visitor'){echo 'selected'} ?>>Visitor</option>
                                 <option value="Professor"<?php if($_SESSION['rol']=='Professor'){echo 'selected'} ?>>Professor</option>
                                 <option value="Student"<?php if($_SESSION['rol']=='student'){echo 'selected'} ?>>Student</option>
                              </select>
                             </div>

and so on.

Lelio Faieta
  • 6,457
  • 7
  • 40
  • 74
  • I tried your code and i get a Notice: Undefined index: rol on this line: `` and it doesn't work. – Just Me Jun 04 '18 at 18:18
  • This is just an example. Adapt it to your variables names. What is the name of the session array where you store the role? – Lelio Faieta Jun 04 '18 at 18:26
  • i saw that after..i'm using `$createpases` – Just Me Jun 04 '18 at 18:44
  • I made it work, thanks a lost for the help, but quick question maybe you can help me with this as well. When i click on a element from my list is autocomplete the form but the same set of parameters appear once again in my list. How can i get only unique set of parameters in my list? – Just Me Jun 04 '18 at 19:21
  • If the answer solve your issue mark it please. For the other request I cannot understand what your issue is – Lelio Faieta Jun 04 '18 at 19:24
  • When i'm completing the form i generate a list on my page with the options that i used like this: `#400000 - times new roman - Landscape - stil 3` `#ff0000 - Arial - Portrait - stil 1` `#ffff00 - comic sans ms - Landscape - stil 2` if i click on one of them they get autocomplete in my form but the same line appears once again in my list like this: `#ff0000 - Arial - Portrait - stil 1` `#ffff00 - comic sans ms - Landscape - stil 2` `#400000 - times new roman - Landscape - stil 3` `#400000 - times new roman - Landscape - stil 3` i want the lines in my list unique if possible – Just Me Jun 04 '18 at 19:32
-1

It appears that your form does not auto-fill with the data because you do not have such a functionality implemented. This auto-complete functionality is not automatic, as in, the server does not do it for you.

Here is an example of what you need to do in order to get the autocomplete to work:

Name:<br> <input type="text" id="name" value="<?= $_SESSION['name'] ?>" /><br>

For the select to auto-complete, you'll need to do something like this:

<select name="cul" id="cul">
    <option value="">- Text color -</option>
    <option value="red" <? echo ($_SESSION['cul']==="red")?"selected='selected'":""; ?>>Red</option>
    <option value="blue" <? echo ($_SESSION['cul']==="blue")?"selected='selected'":""; ?>>Blue</option>
    <option value="black" <? echo ($_SESSION['cul']==="black")?"selected='selected'":""; ?>>Black</option>
</select>

This sets the value for each form element by directly setting the value / selected item using the $_GET data from a previous submission, or if there is no data, everything will be empty / default values.

Per a suggestion from one of the comments to this answer, I would like to point out that you must include "session_start()" to the top of the document if you wish to use the session variables on the document (if you have not done so already).

One question about the original question: is that first code block procedurally generated, or hand-typed?

Brandon Dixon
  • 1,036
  • 9
  • 16
  • 1
    Not true. You need to do it using the values stored in the SESSION array not in the GET one – Lelio Faieta Jun 04 '18 at 15:40
  • Ah, I misunderstood that part of the question, he wants it to persist LONGER than the initial form submission. Using the $_SESSION variables rather than the $_GET variables will do the trick. As to your code quality comment, please explain how you would refactor to improve. – Brandon Dixon Jun 04 '18 at 15:45
  • @BrandonDixon 1) stop using ` ` it is bad practise and discouraged. 2) (You've fixed everything else already) – Martin Jun 04 '18 at 15:50
  • @Martin it seems your statement that is bad practice and discouraged is debatable, as my research, both before and now, seems to show that it isn't simply a large consensus, but has supporters on each side. I am curious as to your opinion on why you would not recommend it. – Brandon Dixon Jun 04 '18 at 15:55
  • you should also mention to start the session to use the SESSION array – Lelio Faieta Jun 04 '18 at 15:55
  • 1
    @BrandonDixon short tags have been deprecated and must be explicitly enabled in php.ini. This is because this can lead to bugs in code. I'm not even sure if it has been removed from PHP 7. See the [manual](http://php.net/manual/en/language.basic-syntax.phptags.php) – Lelio Faieta Jun 04 '18 at 15:56
  • [this topic](https://stackoverflow.com/questions/200640/are-php-short-tags-acceptable-to-use) goes in to it in some detail. short tags `` are discouraged but print tags `=$var` are ok. In summary because short tags are the same as those used for XML – Martin Jun 04 '18 at 19:43
  • see also the [PHP manual for `echo`](http://php.net/manual/en/function.echo.php). Your use of short tags is wrong: there should be *NO SPACE* betwee `` and `=$var` – Martin Jun 04 '18 at 19:45