I'm new at PHP and sessions. For a small project for school. How do i view only the name property of each object stored in a session variable? I tried using foreach and then using $log->name to only get the name property, but it gives me a "Warning: Attempt to read property "name" on string in".
This is where i add new object to session variable:
<?php
class Log
{
public $name;
public $location;
public $activity;
}
if (isset($_POST['submit'])) {
session_start();
$new_log = new Log;
$new_log->name = $_POST['name'];
$new_log->location = $_POST['location'];
$new_log->activity = $_POST['activity'];
$_SESSION['logs'][] = serialize($new_log);
header('Location: data.php');
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<p class="lead">Mensen aanmelden</p>
<form action="index.php" method="POST">
<!-- Vul de form tag aan zodat deze informatie naar process.php zal gepost worden -->
<input type="text" name="name">
<hr>
<input type="radio" name="location" value="inside"> Binnen<br>
<input type="radio" name="location" value="outside"> Buiten
<hr>
<input type="radio" name="activity" value="drinks"> Drinken<br>
<input type="radio" name="activity" value="dance"> Dansen
<hr>
<input type="submit" name="submit" value="Voeg toe">
</form>
</body>
</html>
This is where i want to view each name of the object:
<?php
session_start();
foreach ($_SESSION['logs'] as $log) {
echo $log ->name. "<br>";
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<a href="index.php">return</a>
</body>
</html>