Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6857431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:01:02+00:00 2026-05-27T02:01:02+00:00

Im pretty new to jQuery (since a week) and don’t understand why I can’t

  • 0

Im pretty new to jQuery (since a week) and don’t understand why I can’t assign the PHP (work.php) array to global var imgArrayThumbs. I’m calling an array of image names created by the php opendir function. When I alert(imgArrayThumbs); I’m just getting back [object Object]. Can someone help? Been trying to figure it out all day.

scripts.js

var imgArrayThumbs = '';

$.post("work.php", { task: "imgArrayThumbs" }, function(data) {
  imgArrayThumbs = data;
});

alert(imgArrayThumbs);

work.php

include_once 'scripts/functions.php';

if($_POST['task'] == 'imgArrayThumbs'){
  imageArray('source/examples/thumbs', 'imgArrayThumbs');
}
elseif ($_POST['task'] == 'imgArray'){
  imageArray('source/examples/', 'imgArray');
}

functions.php

function imageArray($dir, $arrayVar){
  $arrayName = array();

  $iNumber = 0;
  $open = opendir ($dir);
  while ($file = readdir( $open )){
    if($file == "." || $file == ".." || $file == ".DS_Store"){

    }else{ 
      $arrayName[$iNumber] = $file;
      $iNumber ++;
    }
  }
  closedir ( $open );

  for ($i = 0;$i<count($arrayName);$i++){ 
    if ($i == 0) { 
      echo "\"" . $arrayName[$i] . "\""; 
    }else{ 
      echo ",\"" . $arrayName[$i] . "\""; 
    } 
  } 
}

EDIT 01

Working version:

scripts.js:

var imgArrayThumbs = new Array();   
$.ajaxSetup({async:false});
$.ajax({
  type: 'POST',
  url: 'work.php',
  data: { task: "imgArrayThumbs" },
  success: function(data) {
    imgArrayThumbs = data;
  },
  dataType: 'json'
});

functions.php:

echo json_encode($arrayName); // without loop
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T02:01:03+00:00Added an answer on May 27, 2026 at 2:01 am

    alert() does a toString(). Since the jQuery code returns an object, alert prints the class type only, [object Object]. Instead, use something like Firebug, or built-in developer tools of IE and Chrome, and do imgArrayThumbs = data; console.log(imgArrayThumbs) then check the Console if you want to browse the contents of the object.

    Also, jQuery expects a JSON result by default, but your PHP code isn’t exactly formatting it in proper JSON. A much simpler solution of encoding it in JSON, is remove your for loop at the end, and use echo json_encode($arrayName); instead.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know how to use jquery alone but since I am pretty new to
I'm pretty new to jQuery and, for my work, I use to take some
I'm pretty new to jQuery, and I am converting a page from regular javascript
I'm pretty new to jQuery/JS. But I have loop in jQuery where I would
I am pretty new to the zend framework and the JQuery helper. I have
i'm pretty new to jquery. I'm trying to insert an element to the DOM,
I'm pretty new to jQuery... so far I'm impressed. But I encountered a situation
I am pretty new to jQuery in general, however the following code works perfectly
I'm pretty new to jQuery and Greasemonkey, but I want to reform a URL.
Pretty new to jquery but i was hoping to be able to target a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.