javascript - php - pass an array from php to jquery ajax and then again pass the same array from jquery to php -


i in situation passing array php jquery ajax using json_encode , saving in empty array declared in jquery script var myarr = [], , later on in same script sending same array i-e, myarr php script through $.ajax using json.stringify function , receiving array in php script json_decode($_post['myarr'], true), problem is not converting array. want receive array can use foreach loop read array.

here code below. first declaring array in jquery script

var imgarr = []; 

then fetching images php script , saving in above declared array

php script:  $getprofileid = $users->getprfid($photoid); $getimages = array(); $getimages = $users->gettypeimage($getprofileid); //echo json_encode($getimages); foreach($getimages $value){    echo json_encode($value);  }  jquery   $.ajax({         type: 'post',         url: 'fetchallimages.php',         data: {'photoid': photoid},         success: function(data){              imgarr = data;           }         }); 

now in same script on other button click sending array imgarr php script using $.ajax. here code:

jquery:  $('#right_arrow').live('click', function(e){  var photoid =   $(this).siblings('#o_popup_post').children('#o_post_box').children("#o_complete_post_image").children("img").attr("id");        $.ajax({        type: 'post',        url: 'nextimage.php',        data: {'photoid': photoid, 'imgarr' : json.stringify(imgarr)},                beforesend: function(){                 $('#o_popup_post').fadeout("normal").remove();                     $('.o_background_popup').append("<div id='o_popup_post'></div>");        },        success: function(response){             $('#o_popup_post').append(response);         // alert(imgarr);         }     });    });       php script:    $photoid = $_post['photoid'];    $imagearray = array();   $imagearray = json_decode($_post['imgarr'], true);    foreach($imagearray $key=>$value){....} 

please help. thanks

i tried example on web server... works expected... , correctly... thing removed beforesend , success javascript functions implementations

i tested , works correctly

html: test.html

    <html>     <head>     <script type="text/javascript" src="//code.jquery.com/jquery-2.1.3.min.js">     </script>     </head>     <body>     <button id="btn">btn</button>     <script type="text/javascript">     $(function() {     $('#btn').on('click', function(evt) {        var photoid = 1;        var imgarr = [{ "name": "photo.jpg", "id": 1 }];        $.ajax({               type: 'post',                  url: 'test.php',                  data: {'photoid': photoid, 'imgarr' : json.stringify(imgarr)},                  beforesend: function(){                  },                  success: function(response){                      alert(imgarr);                 }             });     });     });     </script>     </body>     </html> 

php: test.php

    <?php     //print_r($_post);      $photoid = $_post['photoid'];      $imagearray = array();     $imagearray = json_decode($_post['imgarr'], true);      print_r($imagearray);      foreach($imagearray $key=>$value){      }     ?> 

the $imagearray variable array of arrays shown print_r($imagearray) output:

    array     (         [0] => array             (                 [name] => photo.jpg                 [id] => 1             )      ) 

thus have walk this:

    foreach($imagearray $key=>$value){         echo $value["name"];     } 

or might try function, handles slashes problem in json according magic quotes setting in php:

    function _json_decode($string) {         if (get_magic_quotes_gpc()) {             $string = stripslashes($string);         }          return json_decode($string);     } 

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -