How to store listbox values ​​in an array, then use it to display your data on another php page

I need help on how to store data inserted in a list. Right now I have the following code that creates a list.

    <select name = "lstTerminals" size = "5" id="lstTerminals" multiple="multiple" style="width:200px;">    

    </select>

As you can see, it has no parameters or values. To populate the list, I have a button where it can add values ​​to it depending on what is entered from the text field. Below is a text box in which the user can put text

<input id="txtTerminal" type="text" name="txtTerminal" style="width:115px;"/>

Below is the button code

    <input class = "buttonDesign" id="btnAddTerminal" name ="btnAddTerminal" type="button" onclick = "javaScript:addTerminal();"
        value="Add Terminal" style="width:110px; height:25px"/>

Note that it has an onclick method where, when clicked, it inserts text from text input into its list. The following onclick javaScript:addTerminal()has javascript code:

function addTerminal() {
    var terminal = document.getElementById('txtTerminal');
    var terminals = document.getElementById('lstTerminals');
    var values = new Array();
    var option = document.createElement("option");

    option.value = terminal.value;
    option.text = terminal.value;

    terminals.add(option);

    return true;
}

, , , . : ( ) , , . , , ?

+4
1

var addTerminal php , localStorage.

var selectedTerminals = [];

function addTerminal() {
    var terminal = document.getElementById('txtTerminal');
    var terminals = document.getElementById('lstTerminals');
    var values = new Array();
    var option = document.createElement("option");

    option.value = terminal.value;
    option.text = terminal.value;

    terminals.add(option);
    savedData = {'value':terminal.value,'text':terminal.text};

    selectedTerminals.push(savedData);
    return true;
}

....

- , , js, , localStorage PHP:

function saveBeforeNavigation(){

 if (typeof (Storage) !== 'undefined') {
    // use local storage and save selected terminals in it

  localStorage.setItem('selectedTerminals', JSON.stringify(selectedTerminals));

 } else {
    // post the data to the server and store it to DB / session / file
    $.post('secondPage.php', {'selectedTerminals': selectedTerminals},function(result){if(result.status){alert('saved in session')}else{alert('was not saved in session')}},'json');
 }
}

secondPage.php

<?php
session_start();
// saving posted selectedTerminals
if (isset($_POST['selectedTerminals'])) {
  $_SESSION['selectedTerminals'] = $_POST['selectedTerminals'];
  echo json_encode(array('status' => 1));
  die();
}

// when not posted here you get the selectedTerminal when nav executed
if (isset($_SESSION['selectedTerminals'])) {
  echo '<script type="text/javascript">var selectedTerminals = JSON.parse(\'' . json_encode($_SESSION['selectedTerminals']) . '\')</script>';
}
?>

<script type="text/javascript">
// get the selectedTerminals from the localStorage in case its exists
if(typeof(Storage) !== 'undefined'){
    selectedTerminals = JSON.parse(localStorage.getItem('selectedTerminals'))
}

</script>
+1

All Articles