Wrox Programmer Forums
|
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 September 23rd, 2003, 09:58 AM
Registered User
 
Join Date: Sep 2003
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?

 
Old September 23rd, 2003, 01:22 PM
Registered User
 
Join Date: Sep 2003
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

 
Old September 23rd, 2003, 02:17 PM
Friend of Wrox
 
Join Date: Jun 2003
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/
 
Old September 23rd, 2003, 05:48 PM
Registered User
 
Join Date: Sep 2003
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. ;)


 
Old September 23rd, 2003, 07:17 PM
Friend of Wrox
 
Join Date: Jun 2003
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/
 
Old September 23rd, 2003, 07:21 PM
Friend of Wrox
 
Join Date: Jun 2003
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/
 
Old September 25th, 2003, 07:59 AM
Registered User
 
Join Date: Sep 2003
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...




 
Old September 25th, 2003, 11:50 AM
Friend of Wrox
 
Join Date: Jun 2003
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/
 
Old October 3rd, 2003, 05:54 AM
Registered User
 
Join Date: Sep 2003
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

 
Old March 27th, 2004, 01:09 AM
Registered User
 
Join Date: Mar 2004
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.





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





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