无法从jquery访问数组

人气:501 发布:2022-09-22 标签: php jquery javascript

问题描述

好的,在我之前的问题之前,我已经对我的代码进行了一些修改,但不知怎的,我仍然落后。

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

这是我创建的jquery

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);
               } 
            });
        });
    });

并且addsale.php

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

任何人都可以告诉我访问数组时缺少什么并从addsale.php获取html到当前页面?

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

推荐答案

$(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);
           } 
        });
    });
});

在PHP代码中尝试

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

276