du31992 2016-10-19 03:06 采纳率: 0%
浏览 90
已采纳

无法从jquery访问数组

Okay prior to my previous question I have tried a bit of modification to my code but somehow I am still lagging.

This is jquery I have created

$(document).ready(function()
    {
        var array_ids = [];
        $('.add').click(function()
        {
            array_ids.push($(this).parent().siblings('.row_id').html().trim());
            alert(array_ids);    
        });

        $('.show').click(function(e)
        {
            //e.preventDefault();
            var jsonString = JSON.stringify(array_ids);
            $.ajax(
            {
               method: 'POST',
               url: 'addsale.php',
               data: {data : jsonString},
               cache: false,
               dataType: "json",
               success: function()
               {
                 console.log(data.reply);
                alert(data.reply);
               } 
            });
        });
    });

And addsale.php

if(isset($_POST['push'])) //tried it commenting also!
{
$data = array();
$data = json_decode(stripslashes($_POST['data']));
foreach($data as $d){
 echo $d;
 }
 }

Can anyone tell me what is missing to access the array and get the html from addsale.php to current page?

  • 写回答

4条回答 默认 最新

  • doucongqian6644 2016-10-19 03:13
    关注
    $(document).ready(function()
    {
        var array_ids = [];
        $('.add').click(function()
        {
            array_ids.push($(this).parent().siblings('.row_id').html().trim());
            alert(array_ids);    
        });
    
        $('.show').click(function(e)
        {
            //e.preventDefault();
            //prefer parse function
            var jsonString = JSON.stringify(array_ids);
            $.ajax(
            {
               method: 'POST',
               url: 'addsale.php',
               data: {"data" : jsonString},
               cache: false,
               dataType: "json",
               //e is the response text from your PHP code
               success: function(e)
               {
                 //I don't know why this code
                 //console.log(data.reply);
                //alert(data.reply);
               } 
            });
        });
    });
    

    in your PHP code try

    if(isset($_POST['data'])) //tried it commenting also!
    {
       $data = array();
       $data = json_decode(stripslashes($_POST['data']));
       foreach($data as $d){
           echo $d;
       }
     }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站