Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| 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 September 23rd, 2003, 09:58 AM
Registered User
 
Join Date: Sep 2003
Location: , , .
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default PHP Checkbox

Can anyone help with the checkbox?

I understand how to take the variable across the html form into the php page. If the user ticks the box, the result on the php page is "on".

However, if the user does not tick the box, i get the following error:

Notice: Undefined index: Choice in c:\inetpub\wwwroot\php\Forms\checkbox.php on line 5

Anyony help?

Reply With Quote
  #2 (permalink)  
Old September 23rd, 2003, 01:22 PM
Registered User
 
Join Date: Sep 2003
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It has do to with register_global = off.

You can tackle this by writing:

<?php
if (isset($_POST["Choice"])) echo $_POST["Choice"];
?>

Hope this helps you. :D

Reply With Quote
  #3 (permalink)  
Old September 23rd, 2003, 02:17 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

That's not all it has to do with. Checkboxes are like any other form input -- you can set their value attribute in the tag. The value defaults to "on" if you don't supply one.

Since a checkbox can either be on or off, the value of a checkbox is only submitted if the box is checked.

For more information on register_globals, read my old FAQ:
  http://p2p.wrox.com/archive/beginnin...2002-11/17.asp


For an example of using checkboxes without default values, try this:

<?php

$authors = array('J.R.R. Tolkien',
                 'Dr. Seuss',
                 'Dan Brown',
                 'Robert Ludlum',
                 'Gaston Leroux');

echo "<form action=\"{$_SERVER['PHP_SELF']}\" method=\"post\">\n";

foreach($authors as $author)
{
    echo " <input type=\"checkbox\" value=\"{$author}\" name=\"authors[]\">{$author}</input><br />\n";
}
echo " <input type=\"submit\" value=\"Submit\" name=\"submit\"/>\n";
echo "</form>\n";

echo "\n";


// check submitted values
if (isset($_POST['authors']))
{
   echo "You submitted: " . join(', ', $_POST['authors']) . ".\n";
}

?>



Take care,

Nik
http://www.bigaction.org/
Reply With Quote
  #4 (permalink)  
Old September 23rd, 2003, 05:48 PM
Registered User
 
Join Date: Sep 2003
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Very interesting! You are, of course, very right!

Actually, the problem is if the boxes are checked on the same page there'll be no problems (as you have shown). But, if the on/off status of the checkboxes have to be passed to a secondary page, then the error message will appear (especially if the boxes are not checked). This way the variables have to be controlled/checked.

If everything are to be kept on one page, then not many problems will occur. This matter Joanncae have not yet encountered, because she is still in the third chapter (I know, because I have looked into the 'Beginning PHP') and the application of your 'foreach...' is in the fifth chapter. ;)


Reply With Quote
  #5 (permalink)  
Old September 23rd, 2003, 07:17 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I hate to break it to you, but this is totally incorrect:

Quote:
quote:Originally posted by sebas
Actually, the problem is if the boxes are checked on the same page there'll be no problems (as you have shown). But, if the on/off status of the checkboxes have to be passed to a secondary page, then the error message will appear (especially if the boxes are not checked). This way the variables have to be controlled/checked.

If everything are to be kept on one page, then not many problems will occur. This matter Joanncae have not yet encountered, because she is still in the third chapter (I know, because I have looked into the 'Beginning PHP') and the application of your 'foreach...' is in the fifth chapter. ;)
It doesn't matter if all the logic is on the same page or not. When you submit a form, you're making a completely new request from the web server. The PHP script runs from the top each time, totally unaware of how many times it may have been run for any number of users.

HTTP is a stateless protocol. The closest you can come to maintaining state across page requests is to store variables in cookies or sessions, but those are more advanced topics that we won't discuss today.

Here's another version of the test script just to prove my statements:

<html>
<head><title>Checkbox form</title></head>
<body>

<form method="post" action="form.php">
  <input type="checkbox" name="authors[]" value="J.R.R. Tolkien">J.R.R. Tolkien</input><br />
  <input type="checkbox" name="authors[]" value="Dan Brown">Dan Brown</input><br />
  <input type="checkbox" name="authors[]" value="Gabriel Garcia Marquez">Gabriel Garcia Marquez</input><br />
  <input type="checkbox" name="authors[]" value="F. Scott Fitzgerald">F. Scott Fitzgerald</input><br />
  <input type="checkbox" name="authors[]" value="Nikolai Devereaux">Nikolai Devereaux</input><br />
  <input type="submit" name="submit" value="Submit" />
</body>
</html>


<?php // form.php -- totally separate file from the .html above

if (isset($_POST['authors']))
{
    echo "You chose: " . join(', ', $_POST['authors']) . ".\n";
}

echo "<br />\n";
echo "<a href=\"form.html\">Choose again</a>\n";
?>


The reason you get an undefined index error is not necessarily due to register_globals, it has to do with a new default error_reporting setting. Older versions of PHP suppressed E_NOTICE errors because it was considered a "feature" of PHP that uninitialized variables would be created on-the-fly when needed. The PHP guys finally decided that this was, in fact, a Bad Thing and changed the setting.

For more info on this setting and the reasons behind the change, read:
  http://www.php.net/security.errors


Take care,

Nik
http://www.bigaction.org/
Reply With Quote
  #6 (permalink)  
Old September 23rd, 2003, 07:21 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

One more thing -- I find that it's much easier to use google to search the P2P archives than the built-in search engine. For example:

  http://www.google.com/search?q=site:...or%5Freporting

  http://www.google.com/search?q=site:...ster%5Fglobals


Take care,

Nik
http://www.bigaction.org/
Reply With Quote
  #7 (permalink)  
Old September 25th, 2003, 07:59 AM
Registered User
 
Join Date: Sep 2003
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Well, well, don't worry if you have to break me...
I learn more from you than from those PHP guys!

I don't know the history of PHP. I only know the version from PHP 4.1.x on. So, to me, the reasons why some error statements occur in some PHP versions while others did not is not wholly known to me (I am truly a greenie!).

Are there a book about the history of PHP? Perhaps than it is more clear to me, why some changes in PHP have occurred (for example the "register_global = off"). That would be nice and more understandable...




Reply With Quote
  #8 (permalink)  
Old September 25th, 2003, 11:50 AM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Well, the PHP manual's security section discusses the reasons for changing the default params, as does my FAQ I posted the link to above.
  (http://p2p.wrox.com/archive/beginnin...2002-11/17.asp)

Many PHP books briefly describe the history of the language, as does the PHP manual itself.

Some interesting links:
  http://www.php.net/history
  http://www.webreference.com/programm...php/index.html



Take care,

Nik
http://www.bigaction.org/
Reply With Quote
  #9 (permalink)  
Old October 3rd, 2003, 05:54 AM
Registered User
 
Join Date: Sep 2003
Location: , , .
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hey guys,

I just want to say thanks a lot to everyone who helped me.
I figured out how to do the checkbox now.

Again, thanks very much.

joanncae

Reply With Quote
  #10 (permalink)  
Old March 27th, 2004, 01:09 AM
Registered User
 
Join Date: Mar 2004
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I'm just working my way through the book too, but here is how I got around the lack of a value for the checkbox in chapter 4 (pp121). Using reasoning prsented in the previous posts,of course!

<HTML>
<HEAD></HEAD>
<BODY>
<B>Namllu Car Hire Company</B>
<?php
if (empty($License))
     {
          $License="";
     }
if ($Age>20 AND $License=="on") echo ("Your car hire has been Accepted");
if ($Age<21 OR $License=="") echo ("Unfortunately we cannot hire a car to you.");
?>
</BODY>
</HTML>

No matter where you go..there you are.
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
checkbox checked by default by html:checkbox sachin.tathod Struts 3 December 4th, 2006 03:41 PM
php - checkbox msrinivas PHP How-To 0 March 7th, 2006 03:17 AM
How to insert checkbox values to mysql using php method PHP Databases 0 February 21st, 2006 10:47 PM
Error: movie.php & commit.php on p182-186, ch6 willburke BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 0 October 12th, 2004 02:48 PM
php and javascript checkbox conflict sam PHP How-To 7 June 8th, 2004 06:06 PM



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


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