Alb
Alb

Reputation: 61

A way to get a parameter from the URL

I have this issue i'm trying to solve.

I have this page lista.php In which i load another page (which refreshes every x seconds) named pagination.php

I use this script:

function LoadContent()
{
    $('#lista').load('pagination.php');
}
$(document).ready(function(){
    LoadContent(); //load contentent when page loads
    setInterval('LoadContent()',30000); //schedule refresh
});

Well, there is this URL:

lista.php?id=3

I need the pagination.php to grab the id, but it won't work.. the pagination.php is inside lista.php... how can i solve it? I tried the GET method..

Any suggestion please? Thanks.

Upvotes: 0

Views: 92

Answers (4)

Faridzs
Faridzs

Reputation: 672

i think you can use this:

$('#lista').load('pagination.php?id=<?php echo $_GET['id'] ?>');

Upvotes: 0

Serge S.
Serge S.

Reputation: 4915

To obtain id from your url like lista.php?id=3 you could use

var url = window.location.href;
var queryStr = url.substring(url.indexOf("?")+1);
var id = queryStr.split('=')[1];

So the whole code maybe like:

function LoadContent()
{
    var url = window.location.href;
    var queryStr = url.substring(url.indexOf("?")+1);
    var id = queryStr.split('=')[1];
    $('#lista').load('pagination.php?id=' + id);
}
$(document).ready(function(){
    LoadContent(); //load contentent when page loads
    setInterval('LoadContent()',30000); //schedule refresh
});

Upvotes: 1

Grant Thomas
Grant Thomas

Reputation: 45068

If you're using AJAX to load dynamically then that's a different URL, so append the current query string to the request with something like location.search.substring(url.indexOf("?")).

$('#lista').load('pagination.php' + location.search.substring(url.indexOf("?")));

Upvotes: 0

CompanyDroneFromSector7G
CompanyDroneFromSector7G

Reputation: 4527

Add the parameter to the url, e.g.:

function LoadContent()
{
    var id = ... <== assign id here
    $('#lista').load('pagination.php?id=' + id);
}
$(document).ready(function(){
    LoadContent(); //load contentent when page loads
    setInterval('LoadContent()',30000); //schedule refresh
});

Upvotes: 0

Related Questions