doucuo1642 2018-03-09 23:33
浏览 236

localstorage数据不在页面之间保持不变

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> 
";
    $reEmail = $_POST['reEmail'];
    $reEmail1 = $_POST['reEmail1'];
    //$areport = json_decode($_POST['localStorage.report']);
    $msg = "Incident: " . $incident . "
" . "Name: " . $name . "
" . "Address: 
". $address . "
" . "DOB: " . $dob . "
" . "Gender: " . $gender . "
" . 
$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.

  • 写回答

3条回答 默认 最新

  • doufu7464 2018-03-09 23:36
    关注

    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

    评论

报告相同问题?

悬赏问题

  • ¥15 怎么把多于硬盘空间放到根目录下
  • ¥15 Matlab问题解答有两个问题
  • ¥50 Oracle Kubernetes服务器集群主节点无法访问,工作节点可以访问
  • ¥15 LCD12864中文显示
  • ¥15 在使用CH341SER.EXE时不小心把所有驱动文件删除了怎么解决
  • ¥15 gsoap生成onvif框架
  • ¥15 有关sql server business intellige安装,包括SSDT、SSMS。
  • ¥15 stm32的can接口不能收发数据
  • ¥15 目标检测算法移植到arm开发板
  • ¥15 利用JD51设计温度报警系统