Link to home
Start Free TrialLog in
Avatar of ShaneJones
ShaneJonesFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Posting to a session variable from <a href>

Hi fellow experts,

At the top of the code I have this
if ($_GET['fav']){ $aFav = $_GET['fav']; }

and I have this link under all images.
<a href="http://www.domain.com/gallery/?fav=<?php echo $aFav; ?>2x" title="Favorite this image" class="open" rel="nofollow"></a>

on clicking the images it will add a new variable to the url string

http://www.domain.com/gallery/?fav=2x1x4x7x 
etc

This looks untidy so I would like to know how can I use the <a href> to post the image number to an array but not change the URL from domain.com/gallery/

I would also want to be able to detect if the same image has been added twice so if  2x is in there twice i would like it to not add it as it is already there.

please ignore the 1x 2x naming of the links, this is just for testing until I hook it up to a database.

Thanks for your help

Shane
Avatar of JorisW
JorisW

The easiest way would be to use a redirect (see code snippet for example).
The downside to this method is that after the page is redirected, you no longer know the value of $aFav. However since you are storing that value you can just make note of the latest one added, and use that after the redirect

Other solutions would be to use javascript (for example a hidden POST form, or a XMLHTTPRequest)
<?php
if (isset($_GET['fav']))
{
	$aFav = $_GET['fav'];
	
	/* Do something with $aFav */
	
	$host  = $_SERVER['HTTP_HOST'];
	$uri   = rtrim($_SERVER['PHP_SELF'], '/\\');
	header("Location: http://$host$uri");
	header('Status: 303 See Other');
	exit; 
}
?>
Normal Content

Open in new window

SOLUTION
Avatar of dwaynecharrington
dwaynecharrington
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ShaneJones

ASKER

Thanks for the response. Do you have any examples of how to do this? I normally learn easier by seeing code.

@dwaynecharrington: do you have any more information on step 1? 2 and 3 seem easy enough.

Also how do I get the date to php from JavaScript?

Thanks

Shane
Simple example of using XMLHttpRequest:
The following script assumes that your fav id will never contain any special characters.
// HTML
<a href="http://www.domain.com/gallery/?fav=<?php echo $aFav; ?>2x" title="Favorite this image" class="open" rel="nofollow" onclick="favLinkClick('<?php echo $aFav; ?>2x');return false;"></a>
 
// Javascript
function favLinkClick(favId) 
{
	var http = getHTTPObject(); 
	http.open("GET", "http://www.domain.com/gallery/?fav="+favId, true);
	http.send(null);
}
 
function getHTTPObject()
{
	var xmlhttp;
	/*@cc_on
	@if (@_jscript_version >= 5)
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (E) {
				xmlhttp = null;
			}
		}
	@else
		xmlhttp = false;
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp = null;
		}
	}
	return xmlhttp;
}

Open in new window

Note that my above example is not ideal since it will fetch the entire document for each click.
You could make a second page which is also capable of adding favorites and returns nothing to little.
Also note that the above example also works if javascript is disabled, since it has the href to fall back onto
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi there,

jQuery will not work as I am already using mootools,,,, declaring the jquery script breaks the gallery that is on the page.

Cheers
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ok here is my code so far.

When I put the code that is in Dwayne's response a few points up it just kills the gallery.

http://test.shanedj.com/deborahtarr/gallery/ you can see the gallery there.

What I have now done is created a function that displays the image ID as an alert for the time being.

how now to get that into PHP as a variable to play with.

Thanks for you help so far.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Gallery of Work</title>
<link rel="stylesheet" href="../css/jd.gallery.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../css/main.css" type="text/css" />
<script src="../scripts/mootools.v1.11.js" type="text/javascript"></script>
<script src="../scripts/jd.gallery.js" type="text/javascript"></script>
<script src="../scripts/jd.gallery.set.js" type="text/javascript"></script>
<script src="../scripts/jquery.js" type="text/javascript"></script>
 
<script type="text/javascript">
  jQuery.noConflict();
	
	function startGallery() {
	var myGallery = new gallery($('myGallery'), {
	timed: false
	});
	}
	window.addEvent('domready', startGallery);
	
	function addTo(id){
	alert(id);
	
	}	
</script> 
 
</head>
 
<body>
<div id="myGallery">
    <div class="imageElement">
      <h4>Swans</h4>
      <p>Some Swans by the canal.</p>
<a href="javascript:addTo(1);" title="Favorite this image" class="open" rel="nofollow"></a>
      <img src="../images/brugges2006/1.jpg" class="full" />
    </div>
    <div class="imageElement">
      <h4>Castle</h4>
      <p>Orange lit castle.</p>
      <a href="javascript:addTo(2);" title="Favorite this image" class="open" rel="nofollow"></a>
      <img src="../images/brugges2006/2.jpg" class="full" />
    </div>
</div>
 
</body>
</html>

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Cool seems to be getting somewhere, am guessing that "." means the current page.

How can I call this info and echo it on the page?

<?php echo $_SESSION['fav'] ?>

doesn't seem to work?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Still not echoing it?

http://test.shanedj.com/deborahtarr/gallery/

Is there something else I am missing in the code?

<?php 
session_start();
 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Gallery of Work - Deborah Tarr</title>
<link rel="stylesheet" href="../css/jd.gallery.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../css/main.css" type="text/css" />
<script src="../scripts/mootools.v1.11.js" type="text/javascript"></script>
<script src="../scripts/jd.gallery.js" type="text/javascript"></script>
<script src="../scripts/jd.gallery.set.js" type="text/javascript"></script>
<script src="../scripts/jquery.js" type="text/javascript"></script>
 
<script type="text/javascript">
  jQuery.noConflict();
	
	function startGallery() {
	var myGallery = new gallery($('myGallery'), {
	timed: false
	});
	}
	window.addEvent('domready', startGallery);
	
	function addTo(id){
	jQuery.post(".", { fav: id, date: new Date().getTime() }, 
         function() 
         {
            alert(id);
         }
         );
	}	
</script> 
 
</head>
 
<body>
 
<?php echo $_REQUEST['fav']; ?>
 
<div id="myGallery">
    <div class="imageElement">
      <h4>Swans</h4>
      <p>Some Swans by the canal.</p>
<a href="javascript:addTo(1);" title="Favorite this image" class="open" rel="nofollow"></a>
      <img src="../images/brugges2006/1.jpg" class="full" />
    </div>
    <div class="imageElement">
      <h4>Castle</h4>
      <p>Orange lit castle.</p>
      <a href="javascript:addTo(2);" title="Favorite this image" class="open" rel="nofollow"></a>
      <img src="../images/brugges2006/2.jpg" class="full" />
    </div>
</div>
 
</body>
</html>

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yeah I use firebug, just seen that working. Clever tool that :)

Can it be echo'd on the page tho. or can I even add the word added anywhere?


You would do that in the addTo function (where you had alert(id) in your previous snippet), since the page no longer reloads if you add a favorite because of the XHR.
Am going to close this question and restart a new one so as to distribute the points so far.


Thanks for your help so far, i will open another question for further help and include it in the JS section too this time.