Passing session between pages

here is my requirement. I wanted to pass a variable from one html to another html both has php in it. I know the logic is pretty bad. On clicking the button meant for $row['Room_ID'], it needs to go to plugload6.php . In that php, I wanted to get the Room_ID and handle that in a query. As far as I know, I should do this with onclick listener which calls JS file. In JS file, I should create a session and call the php file. Could you please help me in achieving the same.Please find the code below Main.html:

if(mysqli_num_rows($result)>0)
{
    while($row=mysqli_fetch_assoc($result))
    {
        //$variable=$row['Room_ID'];
        $_SESSION['myVar']=$row['Room_ID'];
        //echo $row['Room_ID']; 
        $newval=$_SESSION['myVar'];
        echo $newval;  
        echo "<tr style='border-bottom: ridge;'>";

        echo "<td align='center'>";
        echo "<a class=".$row['Room_ID']." onclick='plugButton(Room_ID)' href='plugload6.php' class='action-button shadow animate blue' style='color: rgb(255,255,255)'>".$row['Room_ID']."</a>";
        echo "<td>";
        echo "<a href='plugload6.php' class='action-button shadow animate  blue' style='color: rgb(255,255,255)'>".$row['Room_ID']."</a>"; 
        echo "</td>";

plugload6.php:

<?php
//connecting to SQL database plug to retrieve the list of plugs
$room=$_SESSION['myVar'];
echo $room;

$conn=mysqli_connect("localhost","root","") or die("failed");
$db=mysqli_select_db($conn,"splug");
$result=mysqli_query($conn,"select Plug_ID from plug where       Room_ID='".$room."'") or die("failed to connect database".mysql_error());
//$row=mysqli_fetch_array($result);
//count=1;

I wanted to handle in the form of session which can make my life easier. Please guide me in completing the same.

1 answer

  • answered 2017-11-15 01:27 Jure

    Make sure that you started the session with session_start();.

    Or instead of using PHP session pass the Room_ID at the end of URL as a GET parameter

    echo "<a href='plugload6.php?roomId=".$row['Room_ID']."' class='action-button shadow animate  blue' style='color: rgb(255,255,255)'>".$row['Room_ID']."</a>";
    

    And than in plugload6.php use

    $room = $_GET['roomId'];
    

    to get the room id.