Aston
Aston

Reputation: 199

Next and previous button using jquery

I am using a PHP Simple HTML DOM Parser to save a website as a htm file. I then use jQuery to extract a div from the file and put it into a div. I want to create a Previous and Next button but the problem I have is that to do this I have to change the URL so the parser can get the page. I would really appreciate if you can help. Below is the code I am using.

<?php 
    include( 'site/simple_html_dom.php'); 
    $html=file_get_html( 'http://roosterteeth.com/home.php?page=1');
    $html->save('site/rtnews.htm')                      
?>
<script type="text/javascript" src="site/jquery.js"></script>
<script type="text/javascript">
    $('document').ready(function() {
        $('#wrap').click(function (event){
            event.preventDefault();
        });
        $("#wrap").load('site/rtnews.htm #postsArea');    
    });
</script>
</head>
<body>
    <div id="wrap">
    </div>
</body>

Upvotes: 0

Views: 1492

Answers (1)

Jivings
Jivings

Reputation: 23250

You will have to create a new php file for this and make an AJAX request to that file. I assume you have already realised that you cannot make a cross-domain request due to CORS.

Here is your new php file, let's call it proxy.php. It will proxy the request, responding with the page that is passed to it via GET :

<?php
  include( 'site/simple_html_dom.php'); 
  $html=file_get_html( 'http://roosterteeth.com/home.php?page=' . $_GET["page"]);
  echo $html;
?>

Your new JavaScript;

$('document').ready(function() {
  var $wrap = $('#wrap'),
    page = 1;

  $('#next').on('click', function () {
    getPage(++page);
  });

  $('#prev').on('click', function () {
    getPage(--page);
  });

  var getPage = function (page) {
    $wrap.load('proxy.php?page=' + page + ' #postsArea');   
  };

  getPage(page);

});

Upvotes: 1

Related Questions