skip to Main Content

I have a javascript in the head of the index page and this function stores the variables from the functions in localstorage:

<script type="text/javascript">

function storeGlobals() {

localstorage.setItem("name", name.value)
localstorage.setItem("current_age", current_age.value)
localstorage.setItem("retirement_age", retirement_age.value)
localstorage.setItem("base_salary", base_salary.value)
localstorage.setItem("emailid", emailid.value)

localstorage.setItem("FEGLI_BASIC_LIFETIME_COST", "$"+total_FEGLI_BASIC_LIFETIME_COST)
localstorage.setItem("fegli_opa_cost", "$"+total_fegli_opa_cost)
localstorage.setItem("UB_PERMANENT_LIFETIME_COST", "$"+total_UB_PERMANENT_LIFETIME_COST)
localstorage.setItem("fegli_opb_cost", "$"+total_fegli_opb_cost)
localstorage.setItem("UB_TERM_COST", "$"+total_UB_TERM_COST)
localstorage.setItem("Estimated_Total_FEGLI_Lifetime_Cost", "$"+total_Estimated_Total_FEGLI_Lifetime_Cost)

localstorage.setItem("total_savings", "$"+total_savings_final)
localstorage.setItem("regular_finalresult", "$"+regular_total_savings_final)
localstorage.setItem("regular_fegli_basic_coverage", "$"+FEGLI_BASIC_COVERAGE_AMT_report_final)
localstorage.setItem("regular_fegli_opa", "$"+FEGLI_OPA_AMT_report_final)
localstorage.setItem("regular_fegli_opb", "$"+"$"+FEGLI_OPB_AMT_report_final)

}
</script>

Then, in another page (results.php) I have the following in the head tags:

<script type="text/javascript">

function readGlobals() {

localstorage.getItem("name")
localstorage.getItem("current_age")
localstorage.getItem("retirement_age")
localstorage.getItem("base_salary")
localstorage.getItem("emailid")

localstorage.getItem("FEGLI_BASIC_LIFETIME_COST")
localstorage.getItem("fegli_opa_cost")
localstorage.getItem("UB_PERMANENT_LIFETIME_COST")
localstorage.getItem("fegli_opb_cost")
localstorage.getItem("UB_TERM_COST")
localstorage.getItem("Estimated_Total_FEGLI_Lifetime_Cost")

localstorage.getItem("total_savings")
localstorage.getItem("regular_finalresult")
localstorage.getItem("regular_fegli_basic_coverage")
localstorage.getItem("regular_fegli_opa")
localstorage.getItem("regular_fegli_opb")

}
</script>

I am calling this function as a onload event.

Does this look correct? So far, it’s not working. If you are familiar with WYSIWYG Web Builder, what would be the correct way to do this. Any help is appreciated.

Update

I tried one variable and I still cannot get it to work:

<script type="text/javascript">

function readGlobals() {

if document.getElementById("age").value = "13" {
document.getElementById("age").value = "55 through 59";
}

const total_savings = localStorage.getItem("total_savings");
document.getElementById("total_savings").value = total_savings;

}

</script>

2

Answers


  1. Chosen as BEST ANSWER

    I tried one variable and I still cannot get it to work:

    <script type="text/javascript">
    
    function readGlobals() {
    
    if document.getElementById("age").value = "13" {
    document.getElementById("age").value = "55 through 59";
    }
    
    const total_savings = localStorage.getItem("total_savings");
    document.getElementById("total_savings").value = total_savings;
    
    }
    
    </script>
    

  2. you can’t call php variable directly in javascript instead, try the below approach to set the value and get it back based on your already written code
    using or

    <script type="text/javascript">
    
    function storeGlobals() {
    
    localstorage.setItem("name", name.value)
    localstorage.setItem("current_age", current_age.value)
    localstorage.setItem("retirement_age", retirement_age.value)
    localstorage.setItem("base_salary", base_salary.value)
    localstorage.setItem("emailid", emailid.value)
    
    localstorage.setItem("FEGLI_BASIC_LIFETIME_COST", <?php echo  $total_FEGLI_BASIC_LIFETIME_COST ?>)
    localstorage.setItem("fegli_opa_cost",  <?php echo  $total_fegli_opa_cost ?>)
    localstorage.setItem("UB_PERMANENT_LIFETIME_COST",  <?php echo $total_UB_PERMANENT_LIFETIME_COST ?>)
    localstorage.setItem("fegli_opb_cost",  <?php echo $total_fegli_opb_cost ?>)
    localstorage.setItem("UB_TERM_COST",  <?php echo $total_UB_TERM_COST ?>)
    localstorage.setItem("Estimated_Total_FEGLI_Lifetime_Cost",  <?php echo $total_Estimated_Total_FEGLI_Lifetime_Cost ?>)
    
    localstorage.setItem("total_savings",  <?php echo $total_savings_final ?>)
    localstorage.setItem("regular_finalresult",  <?php echo $regular_total_savings_final ?>)
    localstorage.setItem("regular_fegli_basic_coverage",  <?php echo $FEGLI_BASIC_COVERAGE_AMT_report_final ?>)
    localstorage.setItem("regular_fegli_opa",  <?php echo $FEGLI_OPA_AMT_report_final ?>)
    localstorage.setItem("regular_fegli_opb",  <?php echo $FEGLI_OPB_AMT_report_final ?>)
    
    }
    </script>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search