当前位置: 动力学知识库 > 问答 > 编程问答 >

Javascript scope of array and converting js array to a php array

问题描述:

Edit/Clarification: I have a php page, call it displayPhotos.php, in which I set up an Ajax call to a different php page, call it getPhotos.php, which queries a database and returns photo information (caption, file name etc) to displayPhotos.php where they are displayed. I use php in displayPhotos to manipulate the data returned from getPhotos. The returned data from the Ajax call is a javascript 2-dimensional array. I need to turn the javascript array into a php array so I can do they display and other stuff. How do I do that?

Hope this helps.


My eyes hurt from reading all of the docs.

I want to use ajax to query a database, return the data then use php to continue with the web page.

All of the examples I've looked at start with creating the json in php. I need to start with the json object in javascript.

 <script>

var photoarray = [];

var ajaxRequest = $.ajax

({

url : "fhs_get_photos.php",

type: "POST",

dataType: "json",

success: function(photo_array)

{

photoarray = photo_array;

//line below works. the file name is correct but disappears

//outside of this function

console.log("photoarray[0][file_name] is: " + photoarray[0]['file_name']);

},

error: function(request, status, error)

{

alert('An error occurred: ' );

}

});

In this instance I'm not passing anything to the php file that query's the db. The console log shows that the file name in photoarray is correct but once outside of this function it disappears even though it's declared as global, I think it is anyway. Why and what do I need to do to fix this.

The php file just does a SELECT * FROM..... and returns everything.

 // in fhs_get_photos.php

// SELECT * FROM table......

echo $json = json_encode($result);

return $json;

So I now have the array back but it's in javascript. Once I figure out the scope problem, how can I convert this javascript array to a php array?

 <h3>Welcome to the Historical Society's historical photo archive

</h3>

</header>

<figure id="photo_figure">

<script>

//line below gets me: SCRIPT5007: Unable to get property 'thumb' of

//undefined or null reference

console.log("photoarray thumb: ") + photoarray[0]['thumb'];

</script>

Am I explaining this properly?

网友答案:

First of all AJAX is async. This means it sends the request when you ask it to, but receives the response sometime later in the future. And it works after php has rendered and sent the page. So. When you get an update via AJAX, you have to use javascript to make that update matter. The most simple solution is to process the response right there in the success callback. That way you don't need to mess with the global scope (which is a bad practice).

Supposedly, your HTML is like this:

   <header>
     <h3>Welcome to the Historical Society's historical photo archive
     </h3>
   </header>
   <div id="figures"></div>

You can do it by declaring a function that handles the processing:

function updateDom(photoArr) {
  var $figures = $('#figures');

  $.each(photoArr, function() {
    console.log(this);
    $figures.append('<img src="' + this.thumb +'">');
  });
}

Code below is placed in the success callback

And execute that function in the success callback and pass it the array from json, that was parsed and became a valid js object.

var photoArray = JSON.parse(photo_array);
updateDom(photoArray);

here's the fiddle, but it's for the DOM part only

分享给朋友:
您可能感兴趣的文章:
随机阅读: