AJAX-loaded page gives empty $_GET - php

Pages loaded via AJAX methods. index.php is the base page, profile.php is the page loaded
jQuery: ("post" nothing to do with HTML request)
$('<a/>', {href: '?userID='+post['userID']+'#profile'}).text(post['firstName']+' '+post['lastName'])
HTML:
Firstname Lastname
Raw URL (after click)
http://########/#####/index.php?userID=1#home
$_GET print_r on profile.php:
Array ()
By request; the ajax load javascript (index.php):
//AJAX page loading
$(document).ready(function(){
//Default page
if(!window.location.hash)
window.location.hash = '#home';
//Check page reference
checkURL();
//Update nav
$('#main-nav li').on("click", function (){
$('#main-nav li').removeClass('active');
$(this).addClass('active');
//Assign each link a new onclick event, using their own hash as a parameter
checkURL(this.hash);
});
//check for a change in the URL every 250 ms to detect if the history buttons have been used
setInterval("checkURL()",250);
});
//Store the current URL hash
var lasturl="";
function checkURL(hash){
if(!hash)
//if no parameter is provided, use the hash value from the current address
hash=window.location.hash;
if(hash != lasturl) {
//If hash changed, update current hash and load new one
lasturl=hash;
loadPage(hash);
}
}
function loadPage(url) {
//Adjust page name
url=url.replace('#','');
url=url+'.php';
//AJAX load page contents in to main content div
$('#content').load(url);
}

Solution:
Create global storage on root page (index.php).
Set vars in storage instead of using $_GET.
Access vars from child pages.
Example:
index.php (Javascript)
if (typeof(window.storage) === 'undefined') {
window.storagePV = {};
}
$('#nav-profile').click(function() {
window.storage.userID = <?php echo "$_SESSION[userID]"; ?>;
});
accessing userID to view the user's profile page
var userID = window.storage.userID

Related

Set link href as URL with AJAX .load menu

when i click on this menu links i get it on my div, but how can i also set a URL variable so if i click <a href="admin.php"> i get my new url www.foo.com/admin.php i just see the load. if i access directly www.foo.com/admin.php i should get the index.php loading this admin.php and doing the process again, so my admin should also redirect when coming direct at it, someone help me with the way to set the URL with AJAX
var pathname = window.location.pathname;
$("#page-inner").load(pathname+'dashboard.php');
$('#main-menu a').on("click", function (e) {
if (this.href .indexOf('#') == -1) {
e.preventDefault();
$("#page-inner").load(this.href, function () {
}).hide().fadeIn('fast');
return false;
}
});

jquery ajax post onclick - php session variable

On my homepage I'm using the maximage fullscreen image plugin. When someone clicks on a
specific link (product page), I must find out what the current image in the slideshow is
and set it as the background-image in the product index page.
I'm making an ajax call the moment someone clicks the '/producten' link on the homepage and store it as a session variable.
The problem is, it's not making an ajax call, I can't see the POST request in my apache logfile, only the GET request for the '/producten' page. Is it going to fast? Can't I do a POST request just before I make the GET request? I can't pinpoint it. Here's my code:
homepage index:
jQuery(document).ready(function($)
{
$("a[href='/producten']").click(function() {
var best;
var maxz;
$('.mc-image').each(function() {
var z = parseInt($(this).css('z-index'), 10);
if (!best || maxz<z) {
best = this;
maxz = z;
}
});
var bg_image = $(best).css('background-image');
bg_image = bg_image.replace('url(','').replace(')','');
$.post('/producten', {bg_image:bg_image});
});
});
bg_image is set correctly, I tested it with a console.log(), and I get output.
/producten index:
<?php
session_start();
$_SESSION['bg_image'] = $_POST['bg_image'];
?>
In javascript:
/* $.post('/producten', {bg_image:bg_image}); */
this.href = this.href + '?bg_image=' + escape(bg_image)
In php:
if(isset($_GET['bg_image'])) {
$_SESSION['bg_image'] = $_GET['bg_image'];
header('Location: /producten');
exit;
}

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.
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
});
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
});
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("?")));
i think you can use this:
$('#lista').load('pagination.php?id=<?php echo $_GET['id'] ?>');

jquery change content in php template from file with "pages" in

I've created a php template with variables library and what not all pulled into an index page I've created a basic script with fades the page in and out on load and have got that working the next thing I wanted to do is to use my navbar links to pull formatted content into the page (as I'm using foundation 4 framework) now the code I tried is as follows
$(document).ready(function() {
var hash = window.location.hash.substr(1);
var href = $('#nav li a').each(function(){
var href = $(this).attr('href');
if(hash==href.substr(0,href.length-5)){
var toLoad = hash+'.html #content';
$('#content').load(toLoad)
}
});
$('#nav li a').click(function(){
var toLoad = $(this).attr('href')+' #content';
$('#content').hide('fast',loadContent);
$('#load').remove();
$('#wrapper').append('<span id="load">LOADING...</span>');
$('#load').fadeIn('normal');
window.location.hash = $(this).attr('href').substr(0,$(this).attr('href').length-5);
function loadContent() {
$('#content').load(toLoad,'',showNewContent())
}
function showNewContent() {
$('#content').show('normal',hideLoader());
}
function hideLoader() {
$('#load').fadeOut('normal');
}
return false;
});
});
the idea is that onclick it removes the content with the wrapper displays a loading gif and then loads the page content which is stored in the link referred to in the
but its not working is just reloading the whole page . . . . i have tried reading up and it says that you can use
You can extract from another page using the load method thus >$('#targetElement').load('page.htm #container') syntax.
and using the get function but im not to good at all this jquery is there a way I can do it in php or where am I going wrong with what I've done.
Try this....You were calling the load, and functions inside improperly...
$('#nav li a').click(function(){
var toLoad = $(this).attr('href')+' #content';
$('#content').hide('fast',loadContent);
$('#load').remove();
$('#wrapper').append('<span id="load">LOADING...</span>');
$('#load').fadeIn('normal');
//window.location.hash = $(this).attr('href').substr(0,$(this).attr('href').length-5);
function loadContent() {
$('#content').load(toLoad,showNewContent);
}
function showNewContent() {
$('#content').show('normal',hideLoader);
}
function hideLoader() {
$('#load').fadeOut('normal');
}
return false;
});
When a user clicks a link the browser by default unloads the current page and loads the page indicated by that link.
You need to prevent the default behavior of the browser, in order to do this you need to call the .preventDefault() method on the click event that was triggered for that link.
$('#your-context').click(function(event) {
event.preventDefault();
/**
* the rest of your code here
*/
});

load page at its hash, then display the result directly without showing the index first

on http://zentili.koding.com i've got this javascript that loads the content of the linked menu item inside the main #content div of the index page, and applies an hash with the name of the loaded page minus the '.php', otherwise it loads the hash + '.php' if it's entered in the url. works very good. On other hand, the ENG/ITA entries add ?locale=lang_LANG inside the url, right before the hash, so that localization is also working fine. If you look well, you may notice that when you switch between ENG and ITA, the index-content appears just for one moment before going to the hash. I know this is because the page is first loaded, then taken to the hash but i was wondering if there some way for hiding the homepage and going directly to the hash location when it's loaded.
Here the code for my menu:
<script type="text/javascript">
$(document).ready(function() {
var hash = window.location.hash.substr(1);
var href = $('#menubar a.item').each(function(){
var href = $(this).attr('href');
if(hash==href.substr(0,href.length-4)){
var toLoad = hash+'.php';
$('#content').load(toLoad);
$("#menubar a.item").removeClass("current");
$(this).addClass("current");
}
});
$('#menubar a.item').click(function(){
window.location.hash = $(this).attr('href').substr(0,$(this).attr('href').length-4);
var toLoad = $(this).attr('href');
$('#content').fadeOut('fast',loadContent);
function loadContent() {
$('#content').load(toLoad,'',showNewContent) }
function showNewContent() {
$('#content').fadeIn('fast'); }
$("#menubar a.item").removeClass("current");
$(this).addClass("current");
return false;
});
});
function goENG(){
var hash = window.location.hash;
var eng = '?locale=en_EN';
window.location.replace(eng+hash) ;
};
function goITA(){
var hash = window.location.hash;
var ita = '?locale=it_IT';
window.location.replace(ita+hash) ;
};
</script>
the functions goENG() and goITA() are called via onclick on the ENG and ITA a's. I hope to find some solution into this.
The page cannot directly go to the link. It will load in its natural order and then it will go to the hash. For what you want to achieve, there is a simple solution i believe.
Hide the main content div until the document loads. use css rule "visibility:hidden" for this
If there is any hash, load it and then make the content visible.
If there is no hash in url, make the content visible on dom load.
$(document).ready(function() {
var hash = window.location.hash.substr(1);
if ($('#menubar a.item').length > 0) {
var href = $('#menubar a.item').each(function(){
var href = $(this).attr('href');
if(hash==href.substr(0,href.length-4)){
var toLoad = hash+'.php';
$('#content').load(toLoad, function(){
$('#content').attr('visibility', 'visible');
});
$("#menubar a.item").removeClass("current");
$(this).addClass("current");
} else {
$('#content').attr('visibility', 'visible');
}
});
} else {
$('#content').attr('visibility', 'visible');
}
});
--UPDATE--
If you are setting #content as "visibility:hidden"
$('#content').attr('visibility', 'visible');
should always fire, else your #content div will be invisible. The trick here is to set it to visible after we are done with checking for hash. You can keep loading the content in the div and make it visible. Making the #content div visible need not be done after entirely loading the hash.

Categories