0

I'm using a jQuery plugin that uses a hash-url - #/page/1...5

I'm trying to get the page number and use it with PHP without reloading the page.

I tried to send it with AJAX from a javascript variable to a PHP (post) variable but it is redirecting me to another page.

$.ajax({
    url: 'other_page.php',
    type: "POST",
    data: ({id: 1}),
    success: function(data){
        //
    }
}); 

I'm trying to use the id-data (example above) in the same page where the ajax-script is. I hope someone can help me achieve this.

1
  • On what event are you sending this data? Commented Feb 20, 2014 at 14:28

1 Answer 1

1

You can get the hash with

window.location.hash

You must then tokenize it to get the number

var page = window.location.hash.split('/')[2]

You could then forward it on with ajax with

$.ajax({
    url: 'other_page.php',
    type: "POST",
    data: ({id: page}),
    success: function(data){
       //
    }
}); 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, but the problem is that I want to forward it to the same page (with the ajax script), not to another page. Is this possible?
Say your link was <a href="#/page/2/">2</a> clicking that would then update the hash. If you observe the hash change $(window).on('hashchange', function() {}); and put the ajax code above, in that function, it would make a request to the server everytime someone clicks to change the page. You can then populate the appropriate dom element with the result.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.