user2959620
user2959620

Reputation: 29

Ajax PHP: Variable from page url

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.

Upvotes: 0

Views: 2226

Answers (3)

404 Not Found
404 Not Found

Reputation: 1223

  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

Upvotes: 1

Altaf Hussain
Altaf Hussain

Reputation: 1048

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

Upvotes: 0

user399666
user399666

Reputation: 19909

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

Upvotes: 1

Related Questions