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

时间:2018-03-09 23:33:39

标签: javascript php

我正在尝试建立一个网站,以协助记录ems通话期间的事件发生时间。我已经复制了我们的协议,并使用localstorage来记录发生的时间和事件。事件结束后,所有已点击的事件都会显示在报告页面上,可通过电子邮件发送信息。 一切似乎都有效,但是,如果打开另一个页面,localstorage似乎清除,只显示最近页面上的按钮。我需要点击为报告记录的每个按钮。

这是我的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);
  });
});

这是报告页面的一部分:

<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

?>

以下是一个示例按钮:

<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>   

感谢。

3 个答案:

答案 0 :(得分:0)

您没有使用localStorage,只需在全局localStorage变量上设置.report。如果您使用严格模式(js文件顶部的"use strict";),您会看到此问题。

改为使用:

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

并获取项目

localStorage.getItem("report");

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

答案 1 :(得分:0)

这不会将项目设置为localStorage。实际上,localStorage.report未定义。

localStorage.report = JSON.stringify(myItem);

这样做。

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

答案 2 :(得分:0)

我没有将上一页的localStorage添加到新页面。

&#13;
&#13;
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);
});
&#13;
&#13;
&#13;