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 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

Recommended Answers

All 6 Replies

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.

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

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!)

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; is always empty.

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

Thanks

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.

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! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.