doubao6936 2012-06-11 15:31
浏览 41

将php POST数组分配给Javascript数组

I know this may sound similar to some past Q/As, I think mine is slightly different though.. I have a webpage which I want to dynamically load text file information. I upload the text file through an iframe and I want to save this information from php to Javascript. Whenever I try to save this as a regular variable it doesn't work so I have tried to do this by saving this information as a part of the $_POST array under a hidden form named $_POST['hidden_form']. Whenever I try to read the php into Javascript, I keep getting an error "Unexpected token ILLEGAL." I have tried the following two codes:

for($i=0;$i< count($_POST['hidden_form']) ;$i++)
{
  echo "saved_form[$i]='" . $_POST['hidden_form'][$i]. "';
";
}

and saved_form = <?php echo json_encode($_POST['hidden_form']); ?>;

Assigning a php array into a javascript array

I think the error has to do with the " ' " needed to specify the array but not sure. I have no idea where to go from here so any help would be GREATLY appreciated. If there are better methods to do this please let me know. Thanks in advance!

  • 写回答

2条回答 默认 最新

  • dslfjrmz70457 2012-06-11 15:39
    关注
    saved_form = '<?php echo addslashes(json_encode($_POST['hidden_form'])); ?>';
    

    Or

    for($i=0;$i< count($_POST['hidden_form']) ;$i++)
    {
      echo "saved_form[$i]='" . addslashes($_POST['hidden_form'][$i]) . "';
    ";
    }
    

    Both should work, probably had quotes breaking something?

    评论

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型