At first, ensure that $_SESSION[‘devices_id’] exists and is an array:
if(!isset($_SESSION['devices_id'])) {
$_SESSION['devices_id'] = [];
}
The, $_POST[‘devices’] is an array, you can easily merge:
$_SESSION['devices_id'] = array_merge($_SESSION['devices_id'], $_POST['devices']);
I have a Session where I am storing some IDs and I would like to add to the end of that session a string (attach to its own respective id).
Here I have a table with some elements and I have some checkboxes there so when each checkbox is selected I am taking their ids and when posting I am storing those ids to a session:
<form method="post">
<?php if (!empty($arr_devices)) { ?>
<?php foreach ($arr_devices as $device) : ?>
<tr>
<td>
<input type="checkbox" name="devices[]" value="<?php echo $device["id"] ?>">
<?php echo '<a href=error_report.php?device_id=' . urlencode($device["id"]) . "&dev_name=" . urlencode($device["name"]) . "&imei=" . urlencode($device["serial_imei"]) . "&serial_no=" . urlencode($device["serial_no"]) . "&device_no=" . urlencode($device["device_no"]) . '>' . $device["name"] . '</a>'; ?>
</td>
<td>
<?php echo '<a href=error_report.php?device_id=' . urlencode($device["id"]) . "&dev_name=" . urlencode($device["name"]) . "&imei=" . urlencode($device["serial_imei"]) . "&serial_no=" . urlencode($device["serial_no"]) . "&device_no=" . urlencode($device["device_no"]) . '>' . $device["device_no"] . '</a>'; ?>
</td>
<td>
<?php echo '<a href=error_report.php?device_id=' . urlencode($device["id"]) . "&dev_name=" . urlencode($device["name"]) . "&imei=" . urlencode($device["serial_imei"]) . "&serial_no=" . urlencode($device["serial_no"]) . "&device_no=" . urlencode($device["device_no"]) . '>' . $device["barcode"] . '</a>'; ?>
</td>
<td>
<div class="input-group">
<textarea name="dev_comment" placeholder="Write your comment here" rows="1" cols="50"><?php echo $dev_comment; ?></textarea>
</div>
</td>
</tr>
<?php endforeach; ?>
<?php } ?>
<input class="btn" type="submit" name="submit" value="Report">
<form>
Here is the the way I am storing those IDs:
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$_SESSION['devices_id'] = $_POST['devices'];
}
what I would like to do is to add the $dev_comment
to that Session. So that when I use that Session I need to have the $dev_comments attached to each respective id. How can I implement that?
Uh @bhm there is no need for you to make a comment like that. The question of comments + downvotes has been discussed ad nauseum on Meta. Many folks just choose to DV and move on. Many offer advice. Many try to light the path for newbies. This user, as it now stands, no longer exists.