Matthew Matthew - 1 year ago 101
jQuery Question

Insert dynamic content into php page and change the 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
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?

Answer Source

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);


    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 {

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!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download