2017-07-26 101 views
1

我想使用會話發送文章ID,源頁面包含許多鏈接以打開一個網頁,但id在ID屬性中存在href。像將javascript變量轉換爲php並使用會話發送它

<br/> 
< a id="13" class="openpage" href="test.html">open test form< /a><br /> 

我已經使用JavaScript來傳遞變量

$(".openpage").on('click', function() { 
    var artid = $(this).attr("id"); 
    <?php $_SESSION['artid'] = "<script>document.write(artid)</script>"?> 
    }); 


VAR artid的數據類型在加載新的頁面之前,正確的價值$ _SESSION [ 'artid的數據類型']是不是獲得artid的價值。

回答

0

你必須更新你的索引文件和的test.html成爲代碼的 test.php的

jQuery的一部分需要你的ID屬性的值上鍊接 將其存儲在cookie中。

在你test.php的COOKIE的值分配給您的會議和COOKIE使用內置PHP set_cookie() 函數來設置一個空值cookie的清除(出於安全原因);

餅乾1分鐘到期。你可以更新它,如果你喜歡。

-

//index.php文件

<a id="13" class="openpage" href="test.php" >open test form</a> 

<!-- JS HERE ---> 

<script src="http://code.jquery.com/jquery-3.2.1.min.js"></script> 
<script type="text/javascript"> 
    $(document).ready(function(){ 

function setCookie(key, value) { 
      var expires = new Date(); 
      expires.setTime(expires.getTime() + (60 * 1000)); 
      document.cookie = key + '=' + value + ';expires=' + expires.toUTCString(); 

} 

    $(".openpage").on('click', function() { 
     var artid = $('.openpage').attr("id"); 
     setCookie('cookie_session', artid); 
}); 

}); 
</script> 

//test.php文件

<?php 
     if(isset($_COOKIE['cookie_session'])): 
     $_SESSION['artid'] = $_COOKIE['cookie_session']; 
     setcookie("cookie_session", ''); 
     echo $_SESSION['artid']; 
     endif; 
    ?> 

這裏是在瀏覽器的開發工具> 應用的COOKIE的截圖>餅乾

enter image description here

正如你可以看到COOKIE沒有更多的您的瀏覽器和SESSION 現在保存數據。

希望這有助於:)

+0

感謝您的回覆,同時檢查$ _ POST [「yourId」]在test.php的文件,它說不定指標,我沒有得到在test.php的文件中的值:你的ID –

+0

好的。我將採用另一種方法。稍微更新一下。 – Oluwaseye

+0

答案用新方法更新。 – Oluwaseye