0

With the following AJAX call I set pagination for a webpage. It works.

In my PHP file already have:

$page= $_POST[page];

AJAX call:

function pg2(page) {
    pag.ajax({
        type: "POST",
        url: "file.php",
        data: { page: page },
        success: function(ccc) {
            pag("#search_results").html(ccc);
        }
    });
}

I also need to pass id from the URL.

http://website.com/title/?id=2 **//I need to pass id in php file and echo it out.

How can I do that? many thanks.

3
  • Use $_GET[] or $_REQUEST[]; Commented Nov 6, 2013 at 11:40
  • If it's passed in the URL use $_GET['id'] to retrieve it. Commented Nov 6, 2013 at 11:41
  • You can also use .get{} method to do the same Commented Nov 6, 2013 at 11:41

3 Answers 3

1
  var id=<?php echo $_GET['id'];?> // like this you can store php variable in javascript varibale

Now call function pg2(page,id) however you want...

  function pg2(page, id) {
   pag.ajax({
   type: "POST",
   url: "file.php",
   data: { page: page, id: id },
   success: function(ccc) {
    pag("#search_results").html(ccc);
   }
  });
 }

hope it may help you

1

If your JS is embedded:

function pg2(page) {
    var id = <?php echo intval($_GET['id']); ?>;
    pag.ajax({
        type: "POST",
        url: "file.php",
        data: { page: page, id: id },
        success: function(ccc) {
            pag("#search_results").html(ccc);
        }
    });
}

If your JS is in an external file (best option):

var id = <?php echo intval($_GET['id']); ?>;
pg2(page, id);
0

Read id via GET and pass in the function

$id = $_GET['id'];

function pg2(page, id) {
pag.ajax({
    type: "POST",
    url: "file.php",
    data: { page: page, id: id },
    success: function(ccc) {
        pag("#search_results").html(ccc);
    }
});
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.