dongtang2376 2015-06-16 05:11
浏览 120

如何将listbox的值存储到数组中,然后使用它将其数据显示到另一个页面php中

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

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

    </select>

As you can see it has no option or values at all. In order to fill the list, I have a button where it can add values to it depending what is input from a text field. The following is the text field where the user can put text

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

The code below is the button

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

Notice it has an onclick method where when clicked, it will insert the text from the text input into my list box. The following onclick javaScript:addTerminal() has the 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;
}

Once the listbox is filled with text, I should be able to get those text individually and move to another page to display each in a certain way. The question I have now is how can I store all those values (without selecting) inside the list box and maybe store it into an array. Once that is done, how can I call that array and use it into another page to display its values from the previous page?

  • 写回答

1条回答 默认 最新

  • dtt2012 2015-06-16 05:39
    关注

    you can create a global var and save the data in addTerminal function then you can post it to the php or store it in the client local storage depends if the browser supports 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;
    }
    

    ....

    you should have some event when you go to the second page within that event you have to call some js function that decides where data is stored localStorage or PHP session:

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

报告相同问题?

悬赏问题

  • ¥15 uniapp连接阿里云无法发布消息和订阅
  • ¥25 麦当劳点餐系统代码纠错
  • ¥15 轮班监督委员会问题。
  • ¥15 基于作物生长模型下,有限水资源的最大化粮食产量的资源优化模型建立
  • ¥20 关于变压器的具体案例分析
  • ¥15 生成的QRCode圖片加上下載按鈕
  • ¥15 板材切割优化算法,数学建模,python,lingo
  • ¥15 科来模拟ARP欺骗困惑求解
  • ¥100 iOS开发关于快捷指令截屏后如何将截屏(或从截屏中提取出的文本)回传给本应用并打开指定页面
  • ¥15 unity连接Sqlserver