I downloaded the code for the book and then added $_POST and $_COOKIE wherever necessary. But I made a small change and quoted the array indices âtypeâ and âsizeâ in the third and the fourth line as follows:
if ($_POST["type_sel"]) setcookie ("font['type']", $_POST["type_sel"], time()+3600);
if ($_POST["size_sel"]) setcookie ("font['size']", $_POST["size_sel"], time()+3600);
The code did not work and I got the following error message:
âYour cookies say:
Notice: Undefined index: type in c:\inetpub\wwwroot\myweb\cookie_test.php on line 24
FACE=
Notice: Undefined index: size in c:\inetpub\wwwroot\myweb\cookie_test.php on line 26
SIZE=>
Notice: Undefined index: type in c:\inetpub\wwwroot\myweb\cookie_test.php on line 28
$font[type] =
Notice: Undefined index: size in c:\inetpub\wwwroot\myweb\cookie_test.php on line 29
$font[size] =
Your form variables say:
$type_sel = arial
$size_sel = 1â
I finally got it work after taking out the single quotes for the array indices. Also, an example (example 3) at
http://www.zend.com/manual/function.setcookie.php dose not quote the array indices either. But I thought itâs necessary to quote the array index for associative arrays, why did it cause error? Thanks for any help.
The complete code that caused error is as follow:
<?php
//cookie_test.php
if ($_POST["type_sel"]) setcookie ("font['type']", $_POST["type_sel"], time()+3600);
if ($_POST["size_sel"]) setcookie ("font['size']", $_POST["size_sel"], time()+3600);
$type = array("arial", "helvetica", "sans-serif", "courier");
$size = array("1","2","3","4","5","6","7");
echo "<HTML><HEAD><TITLE>Cookie Test</TITLE></HEAD><BODY><DIV ALIGN = 'center'>";
echo "<FORM METHOD=POST>";
echo "What font type would you like to use? ";
echo "<SELECT NAME='type_sel'>";
echo "<OPTION SELECTED VALUE=''>default</OPTION>";
foreach ($type as $var) echo "<OPTION>$var</OPTION>";
echo "</SELECT><BR><BR>";
echo "What font size would you like to use? ";
echo "<SELECT NAME='size_sel'>";
echo "<OPTION SELECTED VALUE=''>default</OPTION>";
foreach ($size as $var) echo "<OPTION>$var</OPTION>";
echo "</SELECT><BR><BR>";
echo "<INPUT TYPE=SUBMIT>";
echo "</FORM>";
echo "<B>Your cookies say:</B><BR>";
echo "";
echo "\$font[type] = " . $_COOKIE["font"]["type"] . "<BR>";
echo "\$font[size] = " . $_COOKIE["font"]["size"] . "<BR>";
echo "<BR>";
echo "<B>Your form variables say:</B><BR>";
echo "";
echo "\$type_sel = " . $_POST["type_sel"] . "<BR>";
echo "\$size_sel = " . $_POST["size_sel"] . "<BR>";
echo "";
echo "</DIV></BODY></HTML>";
?>