将全局变量分配给 jQuery post() 访问的 php 数组

发布于 2024-12-17 06:19:09 字数 1735 浏览 2 评论 0原文

我对 jQuery 还很陌生(一周以来),不明白为什么我不能将 PHP (work.php) 数组分配给全局变量 imgArrayThumbs。我正在调用由 php opendir 函数创建的图像名称数组。当我alert(imgArrayThumbs);时,我只是返回[object Object]。有人可以帮忙吗?一整天都在试图弄清楚。

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] . "\""; 
    } 
  } 
}

编辑01

工作版本:

scripts.js:< /strong>

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

丢了幸福的猪 2024-12-24 06:19:09

alert() 执行 toString()。由于 jQuery 代码返回一个对象,alert 仅打印类类型 [object Object]。相反,使用 Firebug 或 IE 和 Chrome 的内置开发工具,然后执行 imgArrayThumbs = data; console.log(imgArrayThumbs) 如果您想浏览对象的内容,请检查控制台。

另外,默认情况下 jQuery 需要 JSON 结果,但您的 PHP 代码并未完全将其格式化为正确的 JSON。将其编码为 JSON 的一个更简单的解决方案是删除末尾的 for 循环,并使用 echo json_encode($arrayName); 代替。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文