I am new in PHP and don’t have much knowledge about cookies.
It seems that I must store my data in cookies because I just need those data temporarily. I have read some article and tried some of it but I get blank result from it.
here is my code:
<?php
if (isset($_COOKIE['vaccine'])) {
setcookie('vaccine',$vaccine);
foreach ($_COOKIE[$vaccine] as $vaccine){ ?>
<div class="control-group">
<label class="control-label">
<?php echo ' '.'<a href="javascript:void(0);" rel="tooltip"
title="Delete" onclick="delete_vaccination('.$vaccine->vaccination_record_id.');"><i class="icon-minus-sign"></i></a>'.'';?><?php echo $vaccine->vaccination_record_brand;?>
</label>
</div>
<?php }} ?>
You could (should?) use sessions for this. Cookies are not temporary, they are stored on the client’s computer. Sessions are temporary, they are alive until you destroy them.
Furthermore, you have some errors in your PHP (not so much in the cookie handling):
$_COOKIE[$vaccine]will dereference the value of$vaccinein your$_COOKIEarray. As$vaccineseems to be an array, you are looking at$_COOKIE["Array"]. You will want to fix it thus:Also, as you are putting an object in a cookie, you have to serialize/unserialize it, before setting or getting the cookie:
Then, lastly, why do you set the cookie, when the cookie is set? You should read it, when set.
Complete code: