Question

I have this script for money conversion so user can choose it's currency from the list like Us to Euro so I want to make it remain same after page refresh like if user have chosen Euro and he refresh the page it should remain same.

Here is my Javascript and Code

<script>
function updatePrice(val) {
    p = document.getElementById("original_price").value;
    newp = p * val;
    document.getElementById("calculated_price").value = newp;
}

</script>

Php Code:

 <?php  
    $pr = 180;
    ?>
    <select onchange="updatePrice(this.value)">
        <option value="1">US</option>
        <option value="98">RS</option>
        <option value="61">Ind</option>
    </select>
    <input type="hidden" id="original_price" value="<?php echo $pr; ?>" />
    Price: <input type="text" id="calculated_price" value="<?php echo $pr; ?>" />

Update 1 After Implementing Session

<?php
session_start();
// store session data
$_SESSION['value']=".updatePrice(this.value).";

if(isset($_SESSION['value']));

?>

<?php  

$pr = 180;

?>
<select onchange="<?php echo $_SESSION['value']; ?>">
    <option value="1">US</option>
    <option value="98">RS</option>
    <option value="61">Ind</option>
</select>

<br>
<hr>


<input type="hidden" id="original_price" value="<?php echo $pr; ?>" />
Price: <input type="text" id="calculated_price" value="<?php echo $pr; ?>" />


<script>
function updatePrice(val) {
    p = document.getElementById("original_price").value;
    newp = p * val;
    document.getElementById("calculated_price").value = newp;
}

</script>
Was it helpful?

Solution

Actually PHP dosent offer any viewstate mechanism, as far as i know , So what you can do is store this in some hidden field.The best way and my personal recommendation is to use a session variable for this purpose

http://www.w3schools.com/Php/php_sessions.asp

And if you need to solve this issue using javascript, You can use Cookies too

http://www.w3schools.com/js/js_cookies.asp

I have done this using jquery and javascript by setting a cookie, hence i dont want you to get confused with jquery plugin for cookie. You can do this in a much more simpler way using jquery plugin for cookie. Here's the code

HTML

<select id="selectCurrency">
    <option value="1">US</option>
    <option value="98">RS</option>
    <option value="61">Ind</option>
</select>

jquery/javascript

$(document).ready(function(e){
 var name = "Currency=";
 var ca = document.cookie.split(';');
 for(var i=0; i<ca.length; i++){
    var c = ca[i].trim();
    if (c.indexOf(name)==0) $('#selectCurrency').val(c.substring(name.length,c.length));
  }
});

$('#selectCurrency').change(function(e){
    var cookieVal = "Currency="+$(this).val();
    document.cookie = cookieVal ;

  });

Fiddle

http://jsfiddle.net/AmarnathRShenoy/HM3Zj/

OTHER TIPS

You can use session or store the selected values somewhere in database, inUpdate price function make an ajax call which stores, your selected value and keep pdating at, on onchange event. now, each time your page gets refreshed, the previuosly selected value will get fetched from the database and you can show it seleted.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top