1

In my portfolio I implemented a PHP script that generate some div that contain images of some works. When users click on a div, there is a redirect on a details.php page that contains work details. How can I set $_SESSION variable values, in order to use them on details.php?

PHP scipt:

$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>
                        ";
            }
1
  • you start the session and in all pages using them while assign a session array. Commented Mar 24, 2018 at 12:06

3 Answers 3

2

You would need to use JavaScript to detect the onclick and use AJAX to start the session.

For starters, add session_start(); to the top of your page.

Then add an onclick event to your div:

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

Then define the startsession() function:

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:

<?php 
session_start();
$_POST['name'] = $_POST['value'];
<?
Sign up to request clarification or add additional context in comments.

Comments

0
session_start();
//Set session
$name = $_POST['name'];
$_SESSION['user'] = $name;
//Fetch session
echo $_SESSION['user'];

Comments

-1

You can perform an AJAX Request on click of the div. Pass the data in this request that you want to set in the Session variable. Once you get the response back you can then visit the details.php and access the Session variable that got set.

For onclick either you can set

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

Or, set an ID or class and attach an event listener to it.

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

1 Comment

Can i know the reason for down voting my answer?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.