doujiaozhao2489 2016-07-16 03:51
浏览 84
已采纳

将动态内容插入php页面并更改URL

My goal: I have a form that is in parts, 1-4, when the user clicks on the "Next" button I would like the content to animate out then part 2 slides, and so on until the form is complete. The tricky part is I would am trying to use a different php page in a different subfolder to insert as the other 3 parts. This would also change the URL subfolder the user sees.

The working example is actually WordPress. When you click through the multi-part form you will see the content and the URL act as I have described.

I did a bit of digging and it seems like they used React.js on the content but I couldn't really find any documentation on how to do this with React.js so it made me think that maybe it was custom Ajax/jQuery or what.

My Trees of Folders -

  • Main
    • Subfolder-1
      • index.php
    • Subfolder-2
      • index.php

And so on. The only thing I could think of would to use jQuery:

$(document).ready(function() {
  $('#form-container').on('click', '.insert', function() {
    var directory = $(this).attr('name');
    $('#form-container').load('../' + directory);

    return false;
  });
});

I add the class of "insert" on the "Next" button and give it a name="Subfolder-2" $('#form-container').load('../Subfolder-2);' will actually load the content into the div without the page refreshing BUT it does not change the subfolder in the URL.

Am I on the wrong track? Maybe I am just not searching for the right thing?

</div>
  • 写回答

1条回答 默认 最新

  • douzoudang1511 2016-07-19 20:34
    关注

    Ok, so I ended up figuring out how to get the content to act like I wanted with the information John S. provided me. After doing some research and a few hours of trial and error I came up with the JavaScript below:

    var data = 'start',
        url = '../' + data + '/';
        history.pushState(data, null, url);
    

    Above I set the variables and immediately run a history.pushState on page load to capture the first div that is loaded into the content div. This is important because it is the only way I could load the content that happens on initial load back into the page when hitting the browsers back button.

    $('body').on('click', '.insert', function(e) {
    
            data = $(this).attr('data-name'),
            url = '../' + data + '/';
    
            history.pushState(data, null, url);
    
        request_content(data);
    
        return false;
    
    });
    

    Then I add a click listener to the button with the class .insert reset the variables so instead of grabbing the page that initially loaded it grabs the page that will be loaded, then use history.pushState again to change the url that is determined by the variables.

    The request_content function is a simple .load function. So when the button is clicked the variables are set, the url changes and the new content get loaded into the page while the old content disappears.

    The final piece to the puzzle which took me the longest to figure out is actually the popstate function. I am still not 100% sure why it works but after hours of messing with it and finally getting it to work I am not going to question it.

    window.addEventListener('popstate', function(e){
        var data = e.state;
    
        if(data === null) {
    
        } else {
            request_content(data);
        }
    });
    

    This popstate function is what allows the content to come back when hitting the browsers back or forward navigation.

    CSSTricks < this article at CSSTricks helped a TON when learning this method.

    Thanks again to John S. for pointing me in the right direction!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)