Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5
This is the forum to discuss the Wrox book Beginning PHP4 by Wankyu Choi, Allan Kent, Chris Lea, Ganesh Prasad, Chris Ullman; ISBN: 9780764543647
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 30th, 2004, 11:56 AM
Registered User
 
Join Date: Apr 2004
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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>";
?>
 
Old May 1st, 2004, 01:40 AM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Wrr1234: First I don't know how you expect anyone to be able to read this mess! I had to reformat your code so that I could read it. My question is how do you expect yourself to be able to track down errors in sloppy code like that?

Please see:
http://p2p.wrox.com/topic.asp?TOPIC_ID=11967

Code:
<?php
//cookie_test.php

    if (isset($_POST['type_sel']) && !empty($_POST['type_sel']))
    {
        setcookie("font[type]", $_POST['type_sel'], time()+3600);
    }
    if (isset($_POST['size_sel']) && !empty($_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>\n".
         "  <head>\n".
         "      <title>Cookie Test</title>\n".
         "  </head>\n".
         "  <body>\n".
         "      <div align='center'>\n".
         "          <form method='post' action='{$_SERVER['PHP_SELF']}'>\n".
         "              What font type would you like to use?\n".
         "              <select name='type_sel'>\n".
         "                  <option value='' selected='true'>default</option>\n";

    foreach ($type as $var)
    {   
        echo "                  <option value='$var'>$var</option>\n";
    }

    echo "              </select><br /><br />\n".
         "              What font size would you like to use? \n".
         "              <select name='size_sel'>\n".
         "                  <option value=''>default</option>\n";

    foreach ($size as $var)
    {
        echo "                  <option value='$var'>$var</option>\n";
    }

    echo "              </select><br /><br />\n".
         "              <input type='submit' name='action' value='Submit' />\n".
         "          </form>\n";      

    if (isset($_COOKIE['font']['type']) && !empty($_COOKIE['font']['type']) && isset($_COOKIE['font']['size']) && !empty($_COOKIE['font']['size']))
    {
        echo "          <b>Your cookies say: </b><br />\n".
             "          \n".
             "              \$_COOKIE['font']['type'] = ".$_COOKIE['font']['type']."<br />\n".
             "              \$_COOKIE['font']['size'] = ".$_COOKIE['font']['size']."<br />\n".
             "          <br /><br />\n\n";
    }

    if (isset($_POST['type_sel']) && !empty($_POST['type_sel']) && isset($_POST['size_sel']) && !empty($_POST['size_sel']))
    {
        echo "          <b>Your form variables say:</b><br />\n".
             "          \n".
             "              \$_POST['type_sel'] = ".$_POST['type_sel']."<br />\n".
             "              \$_POST['size_sel'] = ".$_POST['size_sel']."<br />\n".
             "          \n";
    }

    echo "      </div>\n".
         "  </body>\n".
         "</html>";

?>
OK, now that I'm done picking on you for formatting, there are a few issues that are causing this to screw up, besides the book's lame example of how to use cookies.

1.) You can't include any quotation marks in the name of the cookie, if you do these are taken literally.

setcookie("font['type']", $_POST['type_sel'], time()+3600);

Creates the variable:
$_COOKIE['font']['\'type\'']

The quotation marks are assumed part of the indice name. You're right that always quoting your associative array indices is a good thing, however there are exceptions to the rule and this is one of them, same thing if you were creating an HTML form with fields named as associative arrays, don't quote the indices. In fact if your array only has a single level of nesting and you're referencing the array within string context e.g. echo " $_POST[hello] "; This is also o.k.

http://www.php.net/manual/en/language.types.string.php

2.) You use variables before they're created, that's why you're getting the E_NOTICE level errors. This is potentially dangerous because you aren't preforming any validation on the data, what I did in the script is a quick hack, if you use isset() and empty() two language constructs that look at a variables existence and its value these E_NOTICE level errors go away.

See:
http://www.php.net/isset
http://www.php.net/empty

If you're ever unsure of a variables value, dump it out! Use something like this:

echo "<pre>";
echo var_dump($_COOKIE);
echo "</pre>";

That would have exposed your quotes being treated as part of the indice.

hth!

Regards,
Rich

::::::::::::::::::::::::::::::::::::::::::
The Spicy Peanut Project
http://www.spicypeanut.net
::::::::::::::::::::::::::::::::::::::::::
 
Old May 3rd, 2004, 01:35 PM
Registered User
 
Join Date: Apr 2004
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi, Richard:
sorry for the messy code, I copied it straight from the book. You were absolute right that the quotation marks were assumed part of the indices names. I spent hours on the Internet looking for information regarding this but failed to find any. Thanks a lot for your help.
 
Old May 27th, 2005, 11:19 AM
Registered User
 
Join Date: May 2005
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Beginning PHP5 Chapter 3, the code given for cookies is still incorrect. Thanks for the information on the fix in this thread. I can move on now as I struggled with trying to fix it myself.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Set-Cookie: PHPSESSID=deleted ajit Beginning PHP 0 October 23rd, 2007 08:29 AM
set the cookie file name crmpicco Javascript How-To 4 February 17th, 2006 05:04 AM
Problem with set cookie chapter 8 stephen_c_ BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 2 December 15th, 2004 02:35 PM
Set Cookie rowlandk Beginning PHP 5 April 6th, 2004 01:18 PM
getting set cookie value from a script abbylee26 Javascript 1 July 17th, 2003 03:01 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.