简体   繁体   English

单击 div 时如何设置 $_SESSION 变量

[英]How to set $_SESSION variable when click on a div

In my portfolio I implemented a PHP script that generate some div that contain images of some works.在我的投资组合中,我实现了一个 PHP 脚本,它生成一些包含一些作品图像的 div。 When users click on a div, there is a redirect on a details.php page that contains work details.当用户单击一个 div 时,会在包含工作详细信息的 details.php 页面上进行重定向。 How can I set $_SESSION variable values, in order to use them on details.php?如何设置 $_SESSION 变量值,以便在 details.php 上使用它们?

PHP scipt: PHP脚本:

$query2="SELECT * FROM Posts WHERE Category='".$record1['Category']."' ORDER BY data_pubb DESC";
            $esito2 = mysql_query($query2);
            while($record2 = mysql_fetch_array($esito2))
            {
                echo "
                    <div class='col-sm-4 job'>
                        <div class='container hover-image-container'>
                            <a href=''>
                                <img src='".$record2['static_img']."' data-src='".$record2['static_img']."' data-animated-src='".$record2['dinamic_img']."' width='100%' height='100%' />
                                <div class='overlay'>
                                    <div class='text'>".$record2['title']."</div>
                                </div>
                            </a>
                        </div>
                    </div>
                        ";
            }

You would need to use JavaScript to detect the onclick and use AJAX to start the session.您需要使用 JavaScript 来检测onclick并使用AJAX来启动会话。

For starters, add session_start();对于初学者,添加session_start(); to the top of your page.到您的页面顶部。

Then add an onclick event to your div:然后向您的 div 添加一个onclick事件:

<div class='col-sm-4 job' onclick='startsession()`>

Then define the startsession() function:然后定义startsession()函数:

function startsession() {
   $.ajax({
      var name = ''; // the name of your session e.g $_SESSION['counters'];
      var value = ''; // the value of your session e.g 10
      url: "sessionstart.php",
      data: {session: name, value: value},
      success: function(data){
         // your success function
      }
   });
}

Then on sessionstart.php :然后在sessionstart.php

<?php 
session_start();
$_POST['name'] = $_POST['value'];
<?
session_start();
//Set session
$name = $_POST['name'];
$_SESSION['user'] = $name;
//Fetch session
echo $_SESSION['user'];

You can perform an AJAX Request on click of the div.您可以通过单击 div 来执行 AJAX 请求。 Pass the data in this request that you want to set in the Session variable.在此请求中传递要在 Session 变量中设置的数据。 Once you get the response back you can then visit the details.php and access the Session variable that got set.收到响应后,您可以访问 details.php 并访问已设置的 Session 变量。

For onclick either you can set对于 onclick,您可以设置

<div ... onclick="functionName()" ></div>

Or, set an ID or class and attach an event listener to it.或者,设置一个 ID 或类并为其附加一个事件侦听器。

<div id="myDiv">...</div>
$('#myDiv').on('click',function(){
   //perform the ajax request here.
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM