将php数组作为变量传递给javascript加载url,然后返回php数组? [英] Passing php array as variables to javascript load url and back to php array?

查看:73
本文介绍了将php数组作为变量传递给javascript加载url,然后返回php数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到许多相关的示例,但我仍然感到困惑.我正在使用ajax(我不太了解)来获取每xxx秒更新一次文件的结果.如果仅传递一个变量,则可以正常工作,但是如果我需要通过php传递数组,那么最好的方法是什么?

I saw many examples related but I'm still confused. I'm using ajax (which I don't know much about) to get the results of a file updated every xxx seconds. It's working perfectly if I pass just one variable, but what is the best way if I need to pass an array from php through it?

结构简单:

show_results.php

<?php
include_once('modWhosonlineCustom.inc.php');
$document->addScript("http://code.jquery.com/jquery-latest.js");
$document->addScript("ajax.js");
$array_name = modWhosonlineCustom::getOnlineUserNames();//the array I need to pass to javascript variable
?>

<script>
var whosonline = '<?php echo "$array_name"; ?>';
</script>

<div id="results"></div>

Ajax代码将在URL加载中构建多个参数:

Ajax code than would have more than one param to build in the url load:

  ajax.js

  $(document).ready(function() {
    $("#results").load("response.php?array_name[param1]&array_name[param2]");
  var refreshId = setInterval(function() {
  $("#results").load("response.php?array_name[param1]&array_name[param2]&randval="+ Math.random());
  }, 10000);
  $.ajaxSetup({ cache: false });
  });

回到PHP响应页面,如何再次使用通过url传递的数组参数?

And back to PHP response page, how could I use again the array params passed through url?

  response.php

  <?php
  $names = $_GET['array_name'];
  foreach ($names as $name) {
  //do something

真的很感谢任何建议,谢谢!

Any suggestions is really appreciated, thanks!

编辑

谢谢大家,我想我现在是正确的方法,但是提出了通过JavaScript中的URL传递此数组的问题.或者,也许我没有以正确的方式在php结束回调文件中得到它.我将向您展示修改后的内容:

Thanks guys, I think I'm the right way now, but ramains the problem to pass this array through a url in the javascript. Or maybe I'm not getting it in the right way in the php end callback file. I'll show you what a modifyed:

   show_results.php

   ...
   <?php
   $names = modWhosonlineCustom::getOnlineUserNames();
   ?>

   <script>
   var whosonline = '<?php echo "json_encode($names)"; ?>';
   </script>


   ajax.js


   $(document).ready(function() {
   $("#atendentes").load("response.php?names=" + whosonline);
   var refreshId = setInterval(function() {
   $("#atendentes").load("response.php?names=" + whosonline + "&randval="+ Math.random());
   }, 10000);
   $.ajaxSetup({ cache: false });
   });


   response.php


   $users = $_GET['names'];
   $users = json_decode($users);
   echo "user: $users";

   $names = $users;
   foreach ($names as $name) {
   ...

在另一面,我得到了:警告:第33行的response.php中为foreach()提供了无效的参数,并且回显为空

Here in the other side I'm getting: Warning: Invalid argument supplied for foreach() in response.php on line 33, and the echo is empty

缺少什么?

推荐答案

您的代码无效.如果有

$arr = array('a', 'b', 'c');
echo $arr;

您实际上得到了

Array

作为输出.不是数组的内容.要将数组从PHP输出"到JS,您必须将其转换为本地Javascript,这是json_encode出现的地方:

as the output. Not the contents of the array. To "output" an array from PHP to JS, you have to convert it to native Javascript, which is where json_encode comes in:

<?php
$arr = array('a', 'b', 'c');
?>
var js_array = <?php echo json_encode($arr) ?>;

会产生

var js_array = ["a","b","c"];

作为一般规则,每当您使用PHP生成JavaScript代码并用PHP值填充Javascript变量时,都应使用json_encode来确保生成有效的Javascript.一旦客户端开始尝试执行它,任何语法错误和整个Javascript代码块都将淹没在水中.

As a general rule, anytime you are using PHP to generate javascript code and are filling in Javascript variables with PHP values, you should use json_encode to ensure that you're generating valid Javascript. Any syntax errors and the whole Javascript code block is dead in the water once the client starts trying to execute it.

这篇关于将php数组作为变量传递给javascript加载url,然后返回php数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆