javascript - 无法从 jquery 访问数组

标签 javascript php jquery

好的,在我上一个问题之前,我已经尝试对我的代码进行一些修改,但不知何故我仍然落后。

这是我创建的 jquery

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

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

谁能告诉我缺少什么来访问数组并将 html 从 addsale.php 获取到当前页面?

最佳答案

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

关于javascript - 无法从 jquery 访问数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40121432/

相关文章:

php - "No tests executed"我根本无法在 NetBeans 中运行任何 PHP 测试

php - 错误插入多个值数据库 php

javascript - 使用多个 '/' 刷新路由会导致 Angular 出错

PHP 使用破折号箭头 "->"检索数组值

javascript - 导致冲突的多个 jquery 无法解决

javascript - 如何让 deferred.promise 正常工作?

php - 如何使用 jQuery 验证插件和 PHP 创建 google reCAPTCHA v2.0 验证

javascript - 当浏览器支持新选项卡时,如何强制 window.open() 打开新选项卡?

javascript - 如何在ReactJS中的导出函数中使用this.state

javascript - <audio> 开始播放时发生的事件