weixin_33720452 2015-03-20 10:04 采纳率: 0%
浏览 33

AJAX-Javascript数组到php

I know that there are so many to find on the internet, however, I have been trying this for hours now, and still not working, Maybe the community could help me out:)

http://jsfiddle.net/dkk2nqyg/14/

I've got a little explanation in the jsfiddle, else, you can take a look in my previous question, JavaScript array splice

Basicly, I want the values from selected ( 3 picked up cards ) in php, because I want to mail those values;)

 $.ajax({
   url: 'data.php',  //I actually want it to be on same page, trying this for debugging
   type: 'post',
   data: {data : selected},
   success: function(data) {
        alert("worked");
   }
});

in the data.php :

<?php
$data = json_decode(stripslashes($_POST['data']));
  foreach($data as $d){
     echo $d;
  }
?>

I would like that I don't even need that data.php but just 1 page, so in the index.php, is that even possible?

Edit: Please, link a JsFiddle, would really help!

  • 写回答

3条回答 默认 最新

  • Memor.の 2015-03-20 10:08
    关注

    First the ajax request should be within the click handler

    $('#mail').click(function () {
        console.log($('#dvDest .flipper').get())
        var selected = $('#dvDest .flipper').map(function () {
            return $(this).data('src')
        }).get();
        alert(selected)
        $.ajax({
            url: 'data.php', //I actually want it to be on same page, trying this for debugging
            type: 'post',
            data: {
                data: selected
            },
            success: function (data) {
                alert("worked");
            }
        });
    })
    

    then in server side loop through the array like(Not sure about the PHP syntax)

    <?php
      foreach($_POST['data'] as $d){
         echo $d;
      }
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改