Jump to content

Setting session variable when clicking an image


Mal1

Recommended Posts

I've got a script which converts currencies based on up to date information and have coded my page so that prices switch between pound, dollar and euro depending on a session variable in the index called currency (defaults as "GBP"). If this changes to "USD" then the dollar price is shown and "EUR" the euro price is displayed. This all works fine but I've got 3 flags for the images and I want to link them so that when they are clicked on it changes the session variable in index.php from:

 

$_SESSION['currency'] = "GBP";

 

to:

 

$_SESSION['currency'] = "USD";

 

or:

 

$_SESSION['currency'] = "EUR";

 

How do/should I go about this?

 

(probably really easy I just don't come from a coding background)

 

 

***EDIT*** Also, just thinking, when one of these images/buttons is clicked the page would probably need to refresh with the new session variable - is there a way to do this?

Link to comment
Share on other sites

You can either capture some POST data or just use a GET variable set in the anchor tag wrapping the images; GET is probably easier given the situation. Just ensure you overwrite the session variable before using it in any of your script else you'll be using the previously set currency.

Link to comment
Share on other sites

Sorry how would I use the Get variable? The images look like this:

 

<img src="http://www.little-persia.com/images/flags/Flag_UK.gif" alt="GBP Currency" border="0" id="GBP" />

 

 

I've also tried this (using CSS to display the forms as images) but it doesn't seem to work:

 

<form enctype="multipart/form-data" action="" method="post">
                        <input type="hidden" name="$_SESSION['currency']" value="EUR"/>
                        <input type="submit" name="EUR" value="" id="EUR-flag"/></form>

Link to comment
Share on other sites

You could set an onClick event for your buttons

 

<a href="#" onClick="runFunctionEuro()">BUTTON</a>

 

Then have javascript code like:

 

<script text/javascript>

function runFunctionEuro(){
self.location.href = "/yourpage.php/?currency=euro";
}
</script>

 

That would reload the page with a query string that you can grab as a GET variable when the page relaods. Something like:

 

<?php
if(isset($_GET['currency'])){
    
     $_session['currency'] = $_GET['currency'];
    
}

?>

 

That's just an example and you could make the javascript bit much better by simply having one function and passing the currency as an argument but it illstrates the point.

 

Also I wouldn't suggest directly using $_GET variables in your code without ensuring they're safe for your purpose.

 

 

Link to comment
Share on other sites

I don't know if that would work with the approach I was taking...

 

I have three different DIVs set up for Euro, GBP and USD. They look like this:

 

<?php if ($_SESSION['currency'] == "GBP") { ?>
<span><p class="info">Price:</p>...

 

So only the div that corresponds to the set session variable will show.

 

What I'm trying to do is change the session variable by clicking on flags which will in turn change what type of currency is displayed on the page.

 

 

Maybe what I'm attempting isn't possible. It was on someone else's suggestion.

Link to comment
Share on other sites

Drongo's approach would work but I think its a little unnecessary.

 

You've already attempt the POST data method but GET would be easier.

 

Just wrap the image in anchor tags, I hate to say it but its not hard.

 

<a href="somePage.php?currency=EUR"><img src="eur.png" /></a>

Link to comment
Share on other sites

Drongo's approach would work but I think its a little unnecessary.

 

You've already attempt the POST data method but GET would be easier.

 

Just wrap the image in anchor tags, I hate to say it but its not hard.

 

<a href="somePage.php?currency=EUR"><img src="eur.png" /></a>

 

That doesn't seem to work. Just takes me back to the home page and then when I go back to product pages the prices are still in GBP (the $_Session['currency'] hasn't been altered).

Link to comment
Share on other sites

You can't copy and paste that HTML and expect it to work. You've gotta write the code to handle the $_GET['currency'] variable; this is assuming you've also replaced the image src and page the href links to.

Link to comment
Share on other sites

I have of course replaced the img src. The href link should either be to index.php (where the session variable is stored) or the current url

"<?php echo $_SERVER['REQUEST_URI'];?>?currency=USD"

I'm not sure which?

 

Not quite sure how to set up the $_GET['currency'] variable? Think I'm getting confused over all the different methods. Or are you saying no longer use session variables?

Link to comment
Share on other sites

Ok, so I've got the form I'm using to work for the three different currencies. It switches between them without issue while on the page:

 

 <div id="country-flags">
           
            <form enctype="multipart/form-data" action="" method="post">
            <input type="hidden" name="currency" value="GBP"/>
            <input type="submit" name="GBP" value="" id="GBP-flag"/>
                </form>
            <form enctype="multipart/form-data" action="" method="post">
            <input type="hidden" name="currency" value="EUR"/>
            <input type="submit" name="EUR" value="" id="EUR-flag"/>
                </form>
            <form enctype="multipart/form-data" action="" method="post">
            <input type="hidden" name="currency" value="USD"/>
            <input type="submit" name="USD" value="" id="USD-flag"/>
                </form> 
</div>

 

This, as far as I can tell posts the session variable to the index.php.

 

Using an IF statement to determine which price to display based on the session variable being GBP, EUR or USD.

 

The code I'm using in the index.php is:

 

$_SESSION['currency'] = $_POST['currency'];
if(!isset($_SESSION['currency'])) $_SESSION['currency'] = "GBP";

 

So this is working (until the user clicks one of the buttons to post a currency GBP is being used, as no currency variable is set) until they navigate to the new page where it appears to lose the variable. Printing the variable on the page shows nothing without the !isset line of code being used or GBP when it is in place.

 

So I seem to be losing the variable when I navigate to the next page. I feel like it's either something simple or trivial I'm missing or I'm trying to do something impossible.

 

Any ideas?

Link to comment
Share on other sites

Sorry dipping into this thread so excuse me if the answer is wrong.

 

But it sounds like you're assuming the form for currency is posting directly to a session variable, which doesn't happen.

 

So it sounds like you need a middle step:

 


//Check is the user has submitted a new currency

if(isset($_POST['currency'])){
$_session['currency'] = $_POST['currency'];  
}

//If the user hasn't changed the currency check if the currency isset, if not, use default value of gbp
else if(!isset($_session['currency'])){
$_session['currency'] = "GBP";
}

 

Typing this directly into the browser so apologies if anything is typoed!

 

Also, and you probably know this, make sure you have session start at the top of each subsequent page to preserve and make accessible the session across pages.

 

Link to comment
Share on other sites

Sorry dipping into this thread so excuse me if the answer is wrong.

 

But it sounds like you're assuming the form for currency is posting directly to a session variable, which doesn't happen.

 

So it sounds like you need a middle step:

 


//Check is the user has submitted a new currency

if(isset($_POST['currency'])){
$_session['currency'] = $_POST['currency'];  
}

//If the user hasn't changed the currency check if the currency isset, if not, use default value of gbp
else if(!isset($_session['currency'])){
$_session['currency'] = "GBP";
}

 

Typing this directly into the browser so apologies if anything is typoed!

 

Thanks, no typos as far as I can see but doing this seems to set the variable to GBP. I don't know if the problem is the forms are resubmitting (blank) every time a new page is loaded?

Link to comment
Share on other sites

Sorry dipping into this thread so excuse me if the answer is wrong.

 

But it sounds like you're assuming the form for currency is posting directly to a session variable, which doesn't happen.

 

So it sounds like you need a middle step:

 


//Check is the user has submitted a new currency

if(isset($_POST['currency'])){
$_session['currency'] = $_POST['currency'];  
}

//If the user hasn't changed the currency check if the currency isset, if not, use default value of gbp
else if(!isset($_session['currency'])){
$_session['currency'] = "GBP";
}

 

Typing this directly into the browser so apologies if anything is typoed!

 

Also, and you probably know this, make sure you have session start at the top of each subsequent page to preserve and make accessible the session across pages.

 

Working! Thanks you're a legend! (Had to change "session" to "SESSION" or at least I think that's why it's now working)

Link to comment
Share on other sites

As far as my logic extends (which on most days isn't that far haha) i believe that snippet of code checks to see if your currency is set in the post array, if not, it will check to see if the currency is session variable is set. If it's not set it will apply the default value of gbp - otherwise it assumes the currency session is set and therefore does nothing.

 

If you think your forms aren't posting try doing:

 

print_r($_POST);

 

at the top of your page. This will display everyting in the post array (again, sorry if i am telling you anything you know). I just tested your form though and it worked perfectly for me.

Link to comment
Share on other sites

As far as my logic extends (which on most days isn't that far haha)...

 

I think the problem was my logic was based on "if it's checking to see if it's not set then it must be checking to see if it's set" which it clearly wasn't. Hard to think the way the code works as we so often go from A to C skipping out B.

Link to comment
Share on other sites

Yeah I know what you mean. That's one thing I've learned - programming is very much a mindset. And very often you solve problems by stepping back and trying to logically think "what's actually going on as this executes..." and then suddenly you have an 'AHA!' moment and realise the issue.

 

Definitely something i can relate to as i have a long way to go on my web development journey hehe...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.