简体   繁体   English

将选择框的选定值作为字符串发送到PHP

[英]send selected values of select box to PHP as string

I'm using Select2 3.4.5 for create select boxes, 我正在使用Select2 3.4.5创建选择框,

I use this code for creatre a Multi-Value Select Boxe and everything is fine. 我使用此代码创建多值选择框,一切都很好。

<select id="e1" name="mydata" multiple>
  <option value="D1">Data1</option>
  <option value="D2">Data2</option>
  <option value="D3">Data3</option>
</select>

...

<script>
     $("#e1").select2();
</script>

For get multiple selected values of select box in php I have to modify name="mydata" by name="mydata[]" , and in PHP I get values by this code: 为了在php中获取选择框的多个选定值,我必须通过name="mydata[]"来修改name="mydata" name="mydata[]" ,而在PHP我可以通过以下代码获取值:

<?php
foreach ($_POST['mydata'] as $names) {
    print "You are selected $names<br/>";
}

?>

But my question: How can I send selected values of select box to PHP as string to recover in php like this : 'D1,D2,D3' , and thanks. 但是我的问题是:如何将选择框的选定值作为字符串发送到PHP,以便在php中像这样恢复: 'D1,D2,D3' ,谢谢。

Edit: 编辑:

I want to send the data as string, not receive it as an array then change it as string 我想将数据作为字符串发送,而不是作为数组接收,然后将其更改为字符串

Server-side with PHP 使用PHP的服务器端

Ideally you would do this with PHP once the value is sent. 理想情况下,一旦发送值,您将使用PHP进行此操作。 To convert the selected items just want to implode the array 要转换所选项目,只想内数组

$names=implode(',', $_POST['mydata']);

Where $_POST['mydata'] is an array $_POST['mydata']是一个数组

[0]=>'D1',
[1]=>'D2',
[2]=>'D3'

implode(',', $_POST['mydata']) would be 'D1,D2,D3' implode(',', $_POST['mydata'])将为'D1,D2,D3'

Client-side with jQuery jQuery客户端

Your title says "send selected values of select box to PHP as string". 您的标题为“将选择框的选定值作为字符串发送到PHP”。 You would do that in JS by catching the submit event of the form and using .join() to change the value of that field. 您可以在JS中通过捕获表单的Submit事件并使用.join()来更改该字段的值来做到这一点。

$('#formid').on('submit', function(){
    $('#e1').val($('#e1').val().join(','));
});

Your form (not given) would need an id <form id="formid" ...> 您的表单(未提供)将需要一个id <form id="formid" ...>

If you want a client-side solution, try getting the val() and calling join() : 如果需要客户端解决方案,请尝试获取val()并调用join()

$('#e1').val().join()

http://jsfiddle.net/gwgLV/ http://jsfiddle.net/gwgLV/

You can do it with javascript. 您可以使用javascript。

 <select id="e1" name="mydata" multiple>
  <option value="D1">Data1</option>
  <option value="D2">Data2</option>
  <option value="D3">Data3</option>
</select>
<button id="but" onclick="now()">Show selected values</button>

javascript code JavaScript代码

function getSelectValues(select) {
      var result = [];
      var options = select && select.options;
      var opt;

      for (var i=0, iLen=options.length; i<iLen; i++) {
           opt = options[i];

           if (opt.selected) {
                 result.push(opt.value || opt.text);
           }
      }
      return result;
 }

 function now(){
     var el =  document.getElementsByTagName('select')[0];
     var x = getSelectValues(el);
     alert(x);
 }

Demo here 在这里演示

Instead of alert store in a variable and send it along with the rest of the form data. 而不是将警报存储在变量中,并将其与表单数据的其余部分一起发送。 Or you can use join (as mentioned in other answers ) to send it over post to php. 或者,您可以使用join(如其他答案中所述)通过发布将其发送到php。

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

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