dongyan7876 2009-12-11 04:29
浏览 68
已采纳

使用JQuery和PHP Session来记住用户的数组

I'm a designer who dabbles in scripting using JQuery and PHP. I have a pretty good understanding of functionality in both but using sessions is new to me.

I have an app with a search feature. The search results may stretch over several pages and each result has a checkbox. The idea is the user goes through the results and checks off items they want, then the result is outputted to a PDF for which there's a button at the bottom of each page. So I need to be able to keep track of the items the user has checked off between pages.

The method I'm using is to keep track of the checked items using an array stored in the $_session. Each time an item is checked JQuery sends the ID to an session.php file using $.post and the idea is that session.php pushes that into the session-stored array, and the opposite for unchecking an item. When the 'convert to pdf' button is clicked JQuery grabs the array from session.php and sends it to another php page that uses the IDs to query and does all the PDF conversion stuff.

Where the gaps in my knowledge fail me are how I can get the array back to JQuery in a usable form. I have it working by sending back and forth a comma delimited string version of the array using this:

foreach($_SESSION["idsArray"] as $value) {
 $sendme .= $value.",";
}
echo $sendme;

I can take that data and break it out in JQuery but I wonder if because of my limited knowledge I'm missing something that would keep the array intact?

Thanks.

  • 写回答

2条回答 默认 最新

  • dtypj3308 2009-12-11 04:38
    关注

    I think, if I understand your question correctly, what you're looking for is json_encode(). If you do a json_encode($_SESSION['idsArray']) then you can fetch that with $.ajax() and set the dataType to 'json'. Then the array will automatically be turned into a JavaScript array.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了