我正在尝试使用AJAX和PHP中的会话来获取以前单击的链接值。但我只获得当前价值。这是我正在使用的代码。
<?php
session_start();
//session_destroy();
if(isset($_POST['getpage'])){
$page = $_POST['pagename'];
$_SESSION['current'] = $page;
if(!isset($_SESSION['previous'])){
$_SESSION['previous'] = "null";
$_SESSION['previousnew'] = $_SESSION['current'];
}
elseif($_SESSION['previous'] == "null"){
$_SESSION['previous'] = $_SESSION['previousnew'];
$_SESSION['previousnew'] = $_SESSION['current'];
}
else
$_SESSION['previous'] = $_SESSION['previousnew'];
print_r($_SESSION);
exit();
}
$value = 5;
$arr = array("file1","file2","file3","file4");
foreach ( $arr as $k => $v){
echo "<a href='".$v.".php' onclick='return createpage(\"$v\");'> ".$v." </a><br>";
}
?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
function createpage (page){
var name = page+".php";
$.post("pageone.php",{getpage : 1,pagename:name},function(data){
alert(data);
});
return false;
}
</script>
更新
<?php
session_start();
//session_destroy();
if(isset($_POST['getpage'])){
$page = $_POST['pagename'];
$_SESSION['current'] = $page;
if(!isset($_SESSION['previous'])){
$_SESSION['previous'] = "null";
$_SESSION['previousnew'] = $_SESSION['current'];
}
elseif($_SESSION['previous'] == "null"){
$_SESSION['previous'] = $_SESSION['previousnew'];
$_SESSION['previousnew'] = $_SESSION['current'];
}
else $_SESSION['previous'] = $_SESSION['previousnew'];
$file_pointer = fopen( $_POST['pagename'], 'w' );
$string = '<?php include_once("pageone.php");?>
<?php $var = "'.$_POST["pagename"].'";?>
<?php echo $var;?>
<?php echo "'.$_SESSION["previous"].'";?>
';
fwrite( $file_pointer, $string );
fclose($file_pointer);
exit();
}
$value = 5;
$arr = array("file1","file2","file3","file4");
foreach ( $arr as $k => $v){
echo "<a href='".$v.".php' onclick='return createpage(\"$v\");'> ".$v." </a><br>";
}
?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
function createpage (page){
var name = page+".php";
$.post("pageone.php",{getpage : 1,pagename:name},function(data){
//alert(data);
});
return true;
}
</script>
在这里,我正在创建新页面,但我无法获取我以前点击过的内容。这就是我想要的东西。
答案 0 :(得分:0)
尝试sessionStorage:
function createpage (page){
if (sessionStorage.getItem('href')) {
alert(sessionStorage.getItem('href'));
}
sessionStorage.setItem('href', $(this).attr('href'));
}