简体   繁体   English

将选择框值存储到数组中

[英]storing select box values into an array

i have two select boxes and a link.i select one value from the first select box and another from the second select box and click on the link.the values have to get stored in an array each time without the previous value getting replaced.how can i do this without using multiple select box? 我有两个选择框和一个link.i从第一个选择框中选择一个值,从第二个选择框中选择另一个,然后单击链接。每次都必须将值存储在一个数组中,而不会更换先前的值。我可以不使用多个选择框吗?

<select name="sq" id="sq" >
<option value=""></option>
</select>

<select name="as" id="as" >
<option value=""></option>
</select>

sorry forgot to mention..its in codeigniter 对不起忘记提及..在codeigniter中

You can use change event to store the selected values. 您可以使用change事件来存储选定的值。

Html HTML

<select name="sq" id="sq" >
   <option value="1">1</option>
   <option value="2">2</option>
</select>

Javascript 使用Javascript

arrSelected = []
$("#sq").change(function(){
     arrSelected.push($(this).val());
});

With the added info from the comments, here is my suggestion: 通过评论中添加的信息,这是我的建议:

HTML: HTML:

<div class="selectLine">
<select name="sq[]" >
<option value=""></option>
</select>

<select name="as[]" >
<option value=""></option>
</select></div>
<a id="addOption">

JavaScript: JavaScript的:

$('#addOption').click(function(){
    $('.selectLine').last().after($('.selectLine').outerHtml());
    $('.selectLine').last().prev().hide();
});

PHP receiving the post: PHP收到帖子:

foreach($_POST['sq'] as $key=>$name){
    //Make sure you stay consistent with the keys to make sure the 2 values were entered at the same time.
    echo '<p>'.$name.' is a '.$_POST['as'][$key].'</p>';
}

Adding [] to the end of the name of inputs will place them in arrays. 将[]添加到输入名称的末尾将把它们放在数组中。 But you need more than one if you want more than one value... 但如果您想要多个值,则需要多个...

You can remove $('.selectLine').last().prev().hide(); 你可以删除$('.selectLine').last().prev().hide(); to keep the lines displayed to the user so he can change the values if you want. 保持显示给用户的线条,以便他可以根据需要更改值。

If you wish to have persistence in your website, then I would recommend looking into PHP Cookies . 如果您希望在您的网站上有持久性,那么我建议您查看PHP Cookie

In your case, you want to store an array, persistently, so you have a few options. 在您的情况下,您希望持久存储数组,因此您有几个选项。

Either implement a HTML Hidden Element or you can use Serialization to store the array inside a cookie. 要么实现HTML隐藏元素,要么使用序列化将数组存储在cookie中。

This would send the data with AJAX without page refresh: 这将使用AJAX发送数据而不刷新页面:

Use for the link <a href="#" id="submitlink">Submit data</a> 用于<a href="#" id="submitlink">Submit data</a>链接

Then add the following jQuery script: (you need to include jQuery library first) 然后添加以下jQuery脚本:(首先需要包含jQuery库)

$('#submitlink').click(function(event) {
    event.preventDefault(); // Stops default link behaviour on click         
    $.ajax({ 
      url: "yourphp.php", // where to send
      data: 'sq=' + $('#sq').val() + '&as=' + $('#as').val(), // select values
      type: "POST",
      success: function(data){
           // If you want to confirm
           alert('Added');
      }
   });
});

Then in your php script store the $_POST data in either a database or session... 然后在你的PHP脚本中将$ _POST数据存储在数据库或会话中......

Session example, storing: 会话示例,存储:

<?php
   session_start();
   if (!isset($_SESSION['sq']) $_SESSION['sq'] = array();
   $_SESSION['sq'][] = $_POST['sq'];

   if (!isset($_SESSION['as']) $_SESSION['as'] = array();
   $_SESSION['as'][] = $_POST['as'];

  ?>

To retrieve the results you could use: 要检索结果,您可以使用:

<?php
   session_start();
   if (isset($_SESSION['sq']) print_r($_SESSION['sq']);
   if (isset($_SESSION['as']) print_r($_SESSION['as']);
?>

But of course this could be elaborated. 但当然可以详细阐述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM