0

I have set an Ajax call for Pagination. I need to pass one more vaiable which is stored in the URL

URL

http://thisite.com/pagetitl/?id=12  **// where 'id=' is a variable I want to pass.**

Ajax Call

function page(page) {
    var dataString = '&page=' + page; // pagination with ajax
    pag.ajax({
        type: "GET",
        url: "cmn_pg.php",
        data: dataString,
        success: function (ccc) {
            pag("#search_results").html(ccc);
        }
    });
}

I have tried to GET it in PHP file $id=$_GET[id], but wont work.

I ask how to pass it with AJAX because I'm quite new to AJAX.

2
  • Try if(isset($_GET['id'])) { $id=$_GET['id']; } Commented Nov 6, 2013 at 9:28
  • If you want to use $_GET then you have to pass it along with the url, that is after 'cmn_pg.php?page='+page Also use $_GET['page'] Commented Nov 6, 2013 at 9:29

5 Answers 5

7

If you are building your query string manually then:

dataString = 'page=' + encodeURIComponent(page);

but you are using jQuery, so don't build it manually:

url: "cmn_pg.php",
data: {
    "page": page
},
success: function (ccc) {

(You also need to use the right name for it in PHP: <?php $id = $_GET['page'] ?>)

Sign up to request clarification or add additional context in comments.

1 Comment

+1 for both covering his errors in his question code and answering the question :)
0

You can pass via url like this

pag.ajax
({
type: "GET",
url: "cmn_pg.php?page="+page,
success: function(ccc)
  {
    pag("#search_results").html(ccc);
  }
});

Or

pag.ajax
({
type: "post",
url: "cmn_pg.php",
data: {'data':dataString},//You can add as many datas seperated by comma to pass more values
success: function(ccc)
  {
    pag("#search_results").html(ccc);
  }
});

And in php

$dataString = $_POST['data'];

Comments

0

You named the variable "page" and try to access it via "id" in PHP. You have to create the query string liek this:

var  dataString = '&id=' + page; 

Alertnitavly you can use pass an object to the "data" parameter andjQUery does the transformationf for you. Sample:

 data: { id: page },

Data to be sent to the server. It is converted to a query string, if not already a string. It's appended to the url for GET-requests. See processData option to prevent this automatic processing. Object must be Key/Value pairs. If value is an Array, jQuery serializes multiple values with same key based on the value of the traditional setting (described below).

Soruce: http://api.jquery.com/jQuery.ajax/

Comments

0

Try this,

pag.ajax({
    type: "GET",
    url: "cmn_pg.php",
    data: {
        page: page, // your page number
        id:12// your id to send
    },
    success: function (ccc) {
        pag("#search_results").html(ccc);
    }
});

Comments

0
 function page(page) {
   var dataString = '&page=' + page; // pagination with ajax pag.ajax
   ({
     type: "GET",
     url: "cmn_pg.php",
     data: {
      page: page
     },
     success: function(ccc) {
       pag("#search_results").html(ccc);
     }
   });

if more data is there to pass add to data variable as given bellow :-
data : {page:page,data2:data2},

Comments

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.