I have notifications on an $_SESSION array: f ex.
11=>'1234-Client A:Message is coming, Ok:0'
14=>'3456-Client B:Message is coming, Not Ok:3'
aso.
I loop through these to print it out in dismissible bootstrap alerts. The last indicator in the message is deciding what alert to use.
When I close a notification I want to close it AND update the $_SESSION array with '...:9' to indicate that this is closed and will not show the next time the page is reloaded. I pass the Id to the JQuery, since this can differ from record to record, but how can I use this on the same PHP-page to change the $_SESSION array for the correct record Id?
I am trying with this code:
invoices.php:
...
$id = $_POST["id"];
echo 'Id: ' . $id . '<br />';
...
<?php if(isset($_SESSION['UploadMessage'])) :
foreach($_SESSION['UploadMessage'] as $key => $val):
$pos = strrpos($val, ":");
$alert = substr($val, $pos+1);
$val = substr($val, 0, $pos);
echo $val . ' ' . $alert . '<br />'; //'
'
$message = $val; ?>
<?php if($alert == 0) : ?>
<div class="alert alert-success alert-dismissible" id="<?=$key;?>" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true" id="<?=$key;?>">×</span></button>
<strong>Ok! </strong><?=$message?>
<a href="#" class="alert-link pull-right" style="float:right,font-size=21px,font-weight:bold,line-height:1,opacity:2">X</a>
</div>
<?php endif; ?>
<?php if($alert == 1) : ?>
<div class="alert alert-info alert-dismissible" id="<?=$key;?>" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true" id="<?=$key;?>">×</span></button>
<strong>Info! </strong><?=$message?>
</div>
<?php endif; ?>
<?php if($alert == 2) : ?>
<div class="alert alert-warning alert-dismissible" id="<?=$key;?>" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true" id="<?=$key;?>">×</span></button>
<strong>Warning! </strong><?=$message?>
</div>
<?php endif; ?>
<?php if($alert == 3) : ?>
<div class="alert alert-danger alert-dismissible" id="<?=$key;?>" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true" id="<?=$key;?>">×</span></button>
<strong>Åtgärda! </strong><?=$message?>
</div>
<?php endif; ?>
<?php endforeach; ?>
<?php endif; ?>
...
<!-- JavaScript -->
<script>
$('button.close').on('click', function (obj) {
var id = obj.target.id;
console.log("Close "+id);
$.post('invoices.php', { 'id': 'id'});
});
</script>
I know that PHP has already loaded the document when the foreach-loop
is running, but how can I pass the value of the array-Id when I click the
close button on each notification and change the array value for this?
Can anyone help a newbie out?
I have tried changing the javascript into this, but it doesn't work. I get the correct id in the alert box, but no update of $_SESSION is made when I reload the page.
// $.post('invoices_ajax.php', { 'id': 'id'});
$.ajax({
type: "POST",
url: 'invoices_ajax.php',
data: ({id:id}),
success: function(data) {
alert(data);
}
});
});
In my Ajax file I have this code:
<?php
$id=$_POST['id'];
echo $id;
if(isset($_SESSION['UploadMessage'])) {
foreach($_SESSION['UploadMessage'] as $k => &$v) {
echo $k . ' ' . $v . '<br />';
if($k == $id) {
$pos = strrpos($v, ":");
$v = substr($v, 0, $pos).':9';
echo $v . '<br />';
}
}
}
?>