954,587 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Setting $_SESSION on image click

Hi

I am currently working on a web site which I had to modify to add the option of either using pounds or euros forthe pricing.

I have two image input types which onclick I want to set the session variable $_SESSION['currency'] to GBP or EURO, and then refresh the page so the new currency is then used.

Do I do something like

onclick="javascript: document.form.currency.value='euro'; document.form.action=\"\"; document.form.submit();"


How do I refresh the page at the same time?

Thanks

Darren

dazlerd
Newbie Poster
6 posts since Feb 2007
Reputation Points: 10
Solved Threads: 0
 

Perhaps something like this.

onclick="javascript: document.form.currency.value='euro'; document.form.action=\"index.php?currency=euro\"; document.form.submit();"

Then in php

<?php
session_start();
if (isset($_GET['currency']) && !empty($_GET['currency'])) {
$_SESSION['currency']=$_GET['currency'];

$v=explode('?','http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'],2);
$v[1]=str_replace('currency='.$_GET['currency'],'',$v[1]);
$v[1]=trim(str_replace('&&','&',$v[1]),'&?');
$j=$v[0];
$j.=(empty($v[1]))?'':'?'.$v[1];
header('Location: '.$j);
exit;
}

That should assign the session it's value on submit then redirect back.

cwarn23
Occupation: Genius
Team Colleague
3,033 posts since Sep 2007
Reputation Points: 413
Solved Threads: 259
 

Hi

Thanks for the reply.

Unfortunately this doesnt work. The onclick is not being called as the page doesnt load index.php.
Here's the full line of code :

<input type="image" src="graphics/currency/pound.jpg" alt="Shop in Pounds" title="Shop in Pounds" id="poundbutton" onmouseover="MM_swapImage('poundbutton','','graphics/currency/pound-over.jpg',1)" onmouseout="MM_swapImgRestore()" onClick="javascript:document.form.currency.value='pound'; document.form.action='index.php?currency=pound'; document.form.submit();"/>


What could be wrong with this. looks like it should work?

Thanks

Darren

dazlerd
Newbie Poster
6 posts since Feb 2007
Reputation Points: 10
Solved Threads: 0
 

But if the form submits data to post, then yes, it wouldn't work. But slightly modified it will:

onclick="javascript: document.form.currency.value='euro'; document.form.action=\"index.php?currency=euro\"; document.form.method=\"get\"; document.form.submit();"


Or you could do this:

<?php
session_start();
if (isset($_GET['currency']) && !empty($_GET['currency'])) {
$_SESSION['currency']=$_POST['currency'];
 
$v=explode('?','http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'],2);
$v[1]=str_replace('currency='.$_GET['currency'],'',$v[1]);
$v[1]=trim(str_replace('&&','&',$v[1]),'&?');
$j=$v[0];
$j.=(empty($v[1]))?'':'?'.$v[1];
header('Location: '.$j);
exit;
}


(If this does solve it, give credit to cwarn, because after all, it's his code!)

FlashCreations
Posting Whiz
395 posts since Sep 2008
Reputation Points: 47
Solved Threads: 47
 

Hi

I must be doing something simple wrong.

In my button press section i have

<form name="currencyform" action="index.php" method="get">
					<input type="image" src="graphics/currency/pound.jpg" alt="Shop in Pounds" title="Shop in Pounds" id="poundbutton" onmouseover="MM_swapImage('poundbutton','','graphics/currency/pound-over.jpg',1)" onmouseout="MM_swapImgRestore()" onClick="javascript:document.currencyform.currency.value='pound'; document.currencyform.action='index2.php?currency=pound'; document.currencyform.submit();"/>
					<input type="image" src="graphics/currency/euro.jpg" alt="Shop in Euros" title="Shop in Euros" id="eurobutton" onmouseover="MM_swapImage('eurobutton','','graphics/currency/euro-over.jpg',1)" onmouseout="MM_swapImgRestore()" onClick="javascript: document.currencyform.currency.value='euro'; document.currencyform.action='index3.php?currency=euro'; document.currencyform.submit();"/>
				</form>


and then in index.php i have

echo "currency = ".$_GET['currency'];
echo "<br/>session currency = ".$_SESSION['currency'];

if (isset($_GET['currency']) && !empty($_GET['currency'])) {
	$_SESSION['currency']=$_GET['currency'];
	//$v=explode('?','http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'],2);
	//$v[1]=str_replace('currency='.$_GET['currency'],'',$v[1]);
	//$v[1]=trim(str_replace('&&','&',$v[1]),'&?');
	//$j=$v[0];
	//$j.=(empty($v[1]))?'':'?'.$v[1];
	//header('Location: '.$j);
	//exit;
}


As you can see the button press is getting index.php from the action of the form not the document submit. index2 and index3 dont exist. Plus the fact $_GET['currency']; is always empty.

What I am doing wrong, ive been looking at this all day and just cant get it to work.

Thanks

dazlerd
Newbie Poster
6 posts since Feb 2007
Reputation Points: 10
Solved Threads: 0
 

Hi
Ive sorted it now.

Things got a little more complex as the code uses Smarty templates.

I call a function with the params of, currency, euro/pound and phpself which returns me the link to the same page so the session variable gets updated.

dazlerd
Newbie Poster
6 posts since Feb 2007
Reputation Points: 10
Solved Threads: 0
 

Awesome! Well if your question has been answered (or solved by some self-changes, which seems to happen a lot! :) ), I ask that you mark the thread as solved. It always bugs me when I visit a thread that has been resolved, but hasn't been mark accordingly! :)

FlashCreations
Posting Whiz
395 posts since Sep 2008
Reputation Points: 47
Solved Threads: 47
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You