Matthew Matthew - 1 year ago 62
Javascript Question

Insert dynamic content into php page and change the url - SOLVED

I apologize in advance just in case this question has been asked already.

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?

I appreciate any helpful answers. Sorry for the book.

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!