Reputation: 59
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 }} ?>
Upvotes: 4
Views: 13722
Reputation: 33511
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 $vaccine
in your $_COOKIE
array. As $vaccine
seems to be an array, you are looking at $_COOKIE["Array"]
. You will want to fix it thus:
foreach ($_COOKIE["vaccine"] as $vaccine)
Also, as you are putting an object in a cookie, you have to serialize/unserialize it, before setting or getting the cookie:
// fetch the vaccines somewhere first
setcookie('vaccine',serialize($vaccines));
Then, lastly, why do you set the cookie, when the cookie is set? You should read it, when set.
Complete code:
<?php
if (isset($_COOKIE['vaccine'])) {
$vaccines = unserialize($_COOKIE['vaccine']);
foreach ($vaccines 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 }} ?>
Upvotes: 2
Reputation: 1885
If that is your entire code then where is the data that goes into $vaccine? It appears that you set that cookie to be empty.
then there are code bugs as well; foreach should have $_COOKIE['vaccine'] instead of $_COOKIE[$vaccine]. But then see below for comments about cookies and arrays.
Additionally you cannot store a PHP array into a cookie. You will have to serialize the array while storing it in cookie, and unserialize it when you get the cookie back, into a PHP array. Either that, or look at example #3 in PHP manual - http://php.net/manual/en/function.setcookie.php on how to deal with arrays in cookies. Be cautious that if you use this way, of using array cookies, then that many cookies are set. Which may be a problem. And so you might want to think about serialization.
One implementation of serialization is given in the setcookie entry in the PHP manual itself. See the comment by 'cwillard at fastmail dot fm' in the manual page is gave above.
Also the cookies that you set would be accessible only on the next page load. Even when you stuff data into $vaccine, the first time around you will see a blank. On page reload you should see wha
Upvotes: 1