如何将数组数据从javascript图像选择器发送到php请求

时间:2018-10-12 01:30:43

标签: javascript php jquery

i使用图像选择器8。选择了多个图片后,我想将输出数据从javascript数据发送到php表单提交,示例代码在这里

HTML / JS

http://rvera.github.io/image-picker/

PHP

<div role="group" aria-label="Image Option Buttons">
  <div class="btn-group">
    <a href="#" id="toggleAll" data-toggle="" aria-pressed="false" autocomplete="off"> Select All</a>
  </div>
</div>

<select multiple='multiple' class='image-picker show-labels show-html hide' name='imageList'>
     <option data-img-src="http://placekitten.com/220/200" name="satu" value="satu">Cute Kitten 1</option>
     <option data-img-src="http://placekitten.com/180/200" value="dua">Cute Kitten 2</option>
     <option data-img-src="http://placekitten.com/130/200" value="3">Cute Kitten 3</option>
     <option data-img-src="http://placekitten.com/270/200" value="4">Cute Kitten 4</option>
</select>

<form method="post" name="myform" action="save.php">
  <input type="hidden" name="data" value="">
  <input type="submit" name="send" value="submit" onclick="imgg()" />
</form>



<script type="text/javascript">
  $(".image-picker").imagepicker({

  });


  function imgg() {
    $("*[multiple=multiple]").find("option:selected").each(function(index, item) {
      var src = $(item).attr("data-img-src");
      document.myform.data.value = src;
      document.forms["myform"].submit();
      console.log(src);
    });

    $('#toggleAll').on('click', function() {
      // click event listener 
      if ($(this).attr('aria-pressed') == 'false') {
        //checks if toggled on or off, any other property can be used
        $('.image-picker').find($('option')).prop("selected", "selected");
        //looks for the image picker option list and sets everything to selected
        $('.image-picker').data('picker').sync_picker_with_select();
        //now triggers the sync function to reinitialise all of the selected images. 

        $('.image-picker').find($('option')).prop("selected", "selected").each(function(index, item) {
            $("submit").click(function() {
              var src = $(item).attr("data-img-src");
              document.myform.data.value = src;
              document.forms["myform"].submit();
              console.log(src);
            });
        });

      } else {
        //does the exact opposite of above. 
        $('.image-picker').find($('option')).prop("selected", false);
        $('.image-picker').data('picker').sync_picker_with_select();
        console.log($(item).attr("data-img-src"));
        $('.image-picker').find($('option')).prop("selected", "selected").each(function(index, item) {

        });
      }

    });

  }
</script>

但这仅显示一个值,例如 if (isset($_POST['data'])) { $names = $_POST['data']; print_r ($names) ; }

我测试了一下,然后看到console.log输出,它可以像显示数组一样工作

http://placekitten.com/270/200

但不是save.php

1 个答案:

答案 0 :(得分:0)

function imgg(){
$("*[multiple=multiple]").find("option:selected").each(function(index, item){ ...

我想我发现问题在于您使用的选择器仅用于“ selected”选项,这也难怪为什么输出值应该只是一个值。