Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old August 20th, 2004, 05:29 PM
Registered User
 
Join Date: Aug 2004
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default variable is not set (chapter 13)

Hello all,
I'm really having torubles trying to figure this one out, I'm wondering if there is an error in the code supplied in the book, or if I am missing something obvious.
My questions refers to the register.php file from Chapter 13.

SOmeone else already asked my first question:

Quote:
quote:
the file either displays a form or registers the users submitted details. the Switch below determines whether the form has been submitted or not:

switch($action) {
   case "register":
      create_account();
   break;
   default:
      html_header();
      register_form();
      html_footer();
   break;
}

i am recieving the following error when i load the file:
Notice: Undefined variable: action in ...

which makes sense to me as the hidden field in the form "action" has not yet been submited.

so why does this code expect the variable "action" to be defined before it is???

Response:
--------------------------------------------------------------------------------

That means $action didn't exist before you tried using it, add a check of the variable's existence before the switch.

if (isset($action))
{
   switch($action) {
      case "register":
         create_account();
      break;
      default:
         html_header();
         register_form();
         html_footer();
   }
}
Now, that all makes sense, but what I dont understand is that if the $action variable isn't set, how can the code in the book get this to work without an If statement?
And the other problem is that the first time the page is opened, $action will not be set, so even register_form() won't execute, meaning that we won't even see the form in the first place!

Am I missing something obvious? SOmeone please enlighten me...
Thanks a lot,
Alex

Reply With Quote
  #2 (permalink)  
Old August 20th, 2004, 05:38 PM
Registered User
 
Join Date: Aug 2004
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Well, I just read the Chapter 12 discussion about the same topic (sorry that I didn't read it earlier), and I used the following:

    switch($_POST["action"])
    {
        case "register":
           DoSignup();
        break;
        default:
                   //do other stuff
        break;
      }

Whcih works, but I'm not sure exactly waht it's doing. If anyone cares to explain, I wold be forever indebted... ;)

Reply With Quote
  #3 (permalink)  
Old August 20th, 2004, 05:52 PM
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

First, have a read of my reply at this thread:
http://p2p.wrox.com/topic.asp?TOPIC_ID=17719

Quote:
quote:
Now, that all makes sense, but what I dont understand is that if the $action variable isn't set, how can the code in the book get this to work without an If statement?
And the other problem is that the first time the page is opened, $action will not be set, so even register_form() won't execute, meaning that we won't even see the form in the first place!
Beginning PHP 4 is a pretty old book, it was written using a very early version of PHP 4. At that time, PHP was much more loose, so the code in the book reflects that. PHP has since evolved and become more explicit.
Code:
switch($action) {
   case "register":
      create_account();
   break;
   default:
      html_header();
      register_form();
      html_footer();
   break;
}
This code takes into account that $action doesn't exist or contain a value by the default case of the switch loop, which means wrapping in if (isset($action)) isn't an appropriate fix here. An appropripriate fix that offers the same functionality is:
Code:
if (!isset($action))
{
    $action = '';
}

switch($action) {
   case "register":
      create_account();
   break;
   default:
      html_header();
      register_form();
      html_footer();
   break;
}
This gives $action a value by initializing it and let's the original code retain its logic. So what's good about this? This goes back to my talk about validation and knowing what values to expect in your variables, which I mentioned in the thread above. By developing with NOTICE level errors you naturally write more explicit and more secure code.

The truth is Beginning PHP4 is outdated! Most of the examples in that book just don't jive with current best practice.

register_globals is another common question, find answers here:
http://p2p.wrox.com/archive/beginnin...2002-11/17.asp

HTH!

Regards,
Rich

--
[http://www.smilingsouls.net]
[http://pear.php.net/Mail_IMAP] A PHP/C-Client/PEAR solution for webmail
Reply With Quote
  #4 (permalink)  
Old August 20th, 2004, 06:13 PM
Registered User
 
Join Date: Aug 2004
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for your quick reply!

I'm glad that there was in fact something wrong with the code, and that it wasn't just me not understanding.

As per your suggestion I may buy the new book, as I have found several issues throughout the book that I am displeased with, although I'm still very glad I own it.

Thanks a lot for your help once again.
Alex

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 13 - large record set causing error with A BasilKBL BOOK: Beginning ASP 3.0 11 May 20th, 2008 11:14 PM
object variable or with block variable not set Aoude BOOK: Beginning VB.NET Databases 1 February 24th, 2006 05:21 PM
Object variable or With block variable not set tparrish Pro VB Databases 1 May 25th, 2005 02:08 PM
Object variable or With block variable not set tparrish Classic ASP Databases 0 May 21st, 2005 06:48 AM
Beginning ASP3.0 chapter 13 - large record set cau BasilKBL ASP.NET 1.0 and 1.1 Basics 2 June 18th, 2004 01:35 PM



All times are GMT -4. The time now is 12:14 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.