dongzhitao4839 2018-10-12 01:30
浏览 45

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

i using image picker http://rvera.github.io/image-picker/ . with multiple selected image ,i want to send output data from javascript data to php form submit, sample code here

HTML/JS

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

PHP

if (isset($_POST['data'])) {

    $names = $_POST['data'];

  print_r ($names) ;
}

but this only show one value like http://placekitten.com/270/200

i tested and see console.log output, it work show array like

http://placekitten.com/220/200

http://placekitten.com/180/200

http://placekitten.com/130/200

http://placekitten.com/270/200

but not for save.php

  • 写回答

1条回答 默认 最新

  • dongsuyou6938 2018-10-12 03:25
    关注
    function imgg(){
    $("*[multiple=multiple]").find("option:selected").each(function(index, item){ ...
    

    I guess I found the problem is that the selector you used is just for "selected" option, no wonder why out put value should be only one value.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。