在两个HTML页面之间传递会话

时间:2019-03-20 04:36:59

标签: javascript php html session

我正在尝试在两个html页面之间传递会话,

$row=mysqli_fetch_assoc($result);
$sname = $row["SP_Name"];
$sname = ucfirst($sname);
$sprice1 = $row["SP_Price1"];
$sprice1 = ucfirst($sprice1);
$_SESSION["sprice1"] = $sprice1;
echo "<tr><td>".'<a  class="btn btn-success" href="pym.html" value="$sprice1">'.$row["SP_Price1"].'</a>'."</tr></td>";

在第二页中,我正在尝试将此值用于js:

<script type="text/javascript">
  var temp = "<?php session_start(); echo($_SESSION['sprice1']); ?>";
  var price = temp;

  var gst = .18;
  var k = Number(price)*Number(gst);
  var total = Number(price)+Number(k)
  document.getElementById("gstprice").innerHTML =k.toFixed(1);
  document.getElementById("total").innerHTML =total.toFixed(1);
  document.getElementById("sprice").innerHTML = price;
</script>

但该值未传递,并且var价格即将变为空

1 个答案:

答案 0 :(得分:0)

在每个使用session['name'];标签的页面上启动会话

<?php
session_start();

$row=mysqli_fetch_assoc($result);
$sname = $row["SP_Name"];
$sname = ucfirst($sname);
$sprice1 = $row["SP_Price1"];
$sprice1 = ucfirst($sprice1);
$_SESSION["sprice1"] = $sprice1;
echo "<tr><td>".'<a  class="btn btn-success" href="pym.html" value="$sprice1">'.$row["SP_Price1"].'</a>'."</tr></td>";
?>

html页面

<?php session_start(); ?> 
<html>
//
//
</html>