Skip to content Skip to sidebar Skip to footer

Cannot Access Array From 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() {

Solution 1:

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

Solution 2:

echo with foreach will not good handling for json response ! You should loop that json after response to ajax success function ...

addsale.php

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

access json resposne in ajax

$.ajax(
        {
           method: 'POST',
           url: 'addsale.php',
           data: {"data" : jsonString},
           cache: false,
           dataType: "json",
           success: function(d)
           {
             //I don't know why this code
             console.log(d);
           } 
        });

Solution 3:

i think you have everything correct but missing something right data: {data: ... }, you should also add something like below-

 data : {data : jsonString, 'push':'push'},

and in php code you try to decode the an array which is not in json format, your code just need to be like this-

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

Solution 4:

No data argument in your success function. Modify your 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();
        var jsonString = JSON.stringify(array_ids);
        $.ajax(
        {
           method: 'POST',
           url: 'addsale.php',
           data: {data : jsonString},
           cache: false,
           dataType: "json",
           success:function(data)
           {
             console.log(data.reply);
             alert(data.reply);
           } 
        });
    });
});

addsale.php

<?php
if(isset($_POST['data'])) //tried it commenting also!
{
$data=json_decode(stripslashes($_POST['data']));
echo json_encode(['reply'=>$data]);
exit();
}

Post a Comment for "Cannot Access Array From Jquery"