douduan6731 2011-07-30 21:35 采纳率: 0%
浏览 30

设置javascript变量时包含PHP文件

I have a project in which different pages on a website will load into one wrapper; when clicking on a page link, the content is that page is loaded into the wrapper instead of the user being taken to a new page. The site is being built with PHP and MySQL, and the page is not supposed to reload. Apparently, it's supposed to be one smooth motion.

I've run through a few options that don't quite cut it. I've considered just including all files and having them within divs with no display, so that when a page is selected JavaScript will update the div display to visible. The problem with this is that it forces ALL of the pages of be parsed, which just means superfluous database queries and a resulting slower page load time.

Another thing that I have tried is to start an AJAX call when the page link is clicked. The JavaScript will pass the page name to a PHP script, which will them set a session variable ,$_SESSION['page_name'], equal to the page name passed. Using this, I can then do something like this:

                            //assume $page is equivalent to $_SESSION['page_name']
            if(isset($page) && file_exists("pages/$page")
            {
                require_once "pages/$page";
            }

But this requires the page to be refreshed in order to work. I am trying to get this all done without the page reloading.

So now I'm a bit stuck. Any suggestions?

  • 写回答

3条回答 默认 最新

  • dsa80833 2011-07-30 21:50
    关注

    Your control point is the client. Have JavaScript set cookies if/when successful ajax responses are triggered. When JS completes an AJAX call, set the cookie. On your click events, check for whatever cookies you need and make the appropriate AJAX calls (or not), depending on whether or not the cookie exists.

    Don't put the responsibility of state handling to your server in this case.

    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?