Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working on a list. The list is displaying all the records from the database into a form.

The form: for each entry on the page1:
session_start()    
foreach ($_SESSION['names'] as $key => $value) {
   $_SESSION['values'] = $value;
   $value = $_SESSION['values']; 
   echo '<form action="../inc/prog/manage-group.php" method="post"">';
   echo '<button name="acceptGroup" value="'.$value.'">accept</button>';
   echo '<button name="declineGroup" value="'.$value.'">decline</button>';
   echo '</form>';
}


When I click on a button (accept or decline) then I want to send a particular $value to the page 2 with the session.

My issue is that it's only show a particular value on the page 2 even tho I click on another button which has a different value

What I have tried:

On the page 2:
session_start();
echo $key[$_SESSION['names']];


Also tried:

session_start();
foreach ($_SESSION['names'] as $key => $value) {
    $value = $_SESSION['names'];
    echo $value;
}
Posted
Updated 21-May-19 10:22am
v3
Comments
phil.o 17-May-19 8:55am    
$_SESSION['values'] = $value;
$value = $_SESSION['values'];

What do you expect from these lines?
Galarist_00 17-May-19 9:02am    
I have to add this to page 2? Can you give a little guide? please!

1 solution

My suggestion for you is send the session item key to page2 and there you check whether the button is accepted or not.

I've created an example for you.

PAGE 1
<?php
    session_start();
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <?php
            $array = array("Mary", "Paul", "John", "Peter");
            $_SESSION['names'] = $array;
                    
            foreach ($_SESSION['names'] as $key => $value) {
                   echo '<form action="page2.php" method="post"">';
                   echo '<input type="hidden" name="action" value="' . $key . '">';
                   echo '<button name="acceptGroup">accept</button>';
                   echo '<button name="declineGroup">decline</button>';
                   echo '</form>';
                }
        ?>
    </body>
</html>


PAGE 2

<?php
session_start();
if(isset($_REQUEST["acceptGroup"]))
{
    echo 'accept';
    echo '<br/>';
}
else 
{
    echo 'decline';  
    echo '<br/>';
}

//get the item through the key
echo $_SESSION['names'][$_REQUEST["action"]];
 
Share this answer
 
v2
Comments
Galarist_00 22-May-19 0:27am    
How can I call a single value from the array with ajax?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900