简体   繁体   中英

localstorage data not persisting between pages

I am attempting to build a website to assist with recording times of events during ems calls. I have replicated our protocols and am using localstorage to record when and what event happens. When the incident is over, all the events that have been clicked are displayed on a report page where the information can be sent via email. Everything seems to work, however, if another page is opened, the localstorage seem to clear and only the buttons clicked on the most recent page appear. I need every button clicked recorded for the report.

This is my JS:

//GO BACK BUTTON
function goBack() {
  window.history.back();
}

// DATE FORMATER
function convertDate() {

  var d = new Date();
  var a = [(d.getMonth() + 1),
    (d.getDate()),
    (d.getFullYear()),
  ].join('-');
  var b = [(d.getHours()),
    (d.getMinutes()),
    (d.getSeconds()),
  ].join(':');
  return (b + ' ' + a);
}

///////////////////button////////////////////////

$(document).ready(function () {
  var report = {};
  var myItem = [];
  $('button').click(function () {
    var itemTime = convertDate() + " ___ " ;
    var clickedBtnID = $(this).text() + " @ " ;
    item = {
      ITEM: clickedBtnID,
      TIME: itemTime ,
    };
    myItem.push(item);
    localStorage.report = JSON.stringify(myItem);
  });
});

And this is part of the report page:

<script>
window.onload = function () {
var areport = JSON.stringify(localStorage.report);
console.log(areport);
areport = areport.replace(/\\"/g, "");
areport = areport.replace(/{/g, "");
areport = areport.replace(/}/g, "");
areport = areport.replace(/[\[\]']+/g, "");
areport = areport.replace(/,/g, "");
areport = areport.replace(/"/g, "");
console.log(areport);
document.getElementById('result').innerHTML = areport;
};
</script>
<body>
<div class="container-fluid">
<h1>Report</h1>
<?php
if (isset($_POST["send"])) {
$incident = $_POST['incident'];
$name = $_POST['name'];
$address = $_POST['address'];
$dob = $_POST['dob'];
$gender = $_POST['gender'];
$reportData = $_POST['reportData'];

if ($incident == '') {
    echo $incident = 'No incident number entered.';
    echo '<br>';
} else {
    echo $incident . '<br>';
}
if ($name == '') {
    echo $name = 'No name entered.';
    echo '<br>';
} else {
    echo $name . '<br>';
}
if ($address == '') {
    echo $address = 'No address entered.';
    echo '<br>';
} else {
    echo $address . '<br>';
}
if ($dob == '') {
    echo $dob = 'No birthdate entered.';
    echo '<br>';
} else {
    echo $dob . '<br>';
}
if ($gender == '') {
    echo $gender = 'No gender entered.';
    echo '<br>';
} else {
    echo $gender . '<br>';
}
if ($reportData == null) {
    echo $reportData = 'No report entered.';
        echo '<br>';
    } else {
        echo $reportData . '<br>';
    }

    //mail
   $headers = "From: CCEMP.info <ccemlbaw@server237.web-hosting.com> \r\n";
    $reEmail = $_POST['reEmail'];
    $reEmail1 = $_POST['reEmail1'];
    //$areport = json_decode($_POST['localStorage.report']);
    $msg = "Incident: " . $incident . "\n" . "Name: " . $name . "\n" . "Address: 
". $address . "\n" . "DOB: " . $dob . "\n" . "Gender: " . $gender . "\n" . 
$reportData;
    mail($reEmail, 'Incident:' . $incident, $msg, $headers);
    mail($reEmail1, 'Incident:' . $incident, $msg, $headers);
}//end of submit

?>

Here is a sample button:

<div class="dropdown">
      <button class="btn btn-secondary dropdown-toggle" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">General Truama</button>
      <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
        <a class="dropdown-item" >Continue Assessment</a>
        <a class="dropdown-item" href="GATA.php">Go to Truama</a>
      </div>
    </div>   

Thanks.

You are not using localStorage, just setting .report on global localStorage variable. You would see this issue if you used strict mode ( "use strict"; at top of the js file).

instead use:

localStorage.setItem("report", JSON.stringify(myItem));

and to get the item

localStorage.getItem("report");


https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage

This does not set the item to localStorage . In fact, localStorage.report is undefined.

localStorage.report = JSON.stringify(myItem);

This does.

localStorage.setItem('report', JSON.stringify(myItem));

I wasn't adding the localStorage from the previous page to the new one.

 var myItem = []; $(document).ready(function () { $('button').click(function () { var itemTime = convertDate() + " &#95;&#95;&#95; " ; var clickedBtnID = $(this).text() + " &#64; " ; var item = { ITEM: clickedBtnID, TIME: itemTime , }; myItem.push(item); localStorage.setItem('report', JSON.stringify(myItem)); console.log(myItem); }); var areport = localStorage.getItem("report"); myItem.push(areport); }); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM